So basically, I've had an assignment that was recreating the application layer by myself (kind of creating HTTP on my own) So I've been given some simple web files, which include box.js, jquery.min.js, and submit.js everything is sent as it should, and for some reason, only jquery.min.js is sent as an empty file.
I've checked and I do send the file data, and I've even tested it with my own pythonic client (code:)
import socket
IP = "127.0.0.1"
PORT = 80
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((IP, PORT))
client_socket.send(r'GET js/jquery.min.js http/1.1\r\n'.encode())
print(client_socket.recv(1024))
Any ideas about what could be the cause? ( keep in mind I've checked and printed the data I send, and it seems to be alright, but the client sees it as an empty file)
My server code:
def handle_client_request(resource, client_socket):
""" Check the required resource, generate proper HTTP response and send to client"""
# TO DO : add code that given a resource (URL and parameters) generates the proper response
if resource.startswith("/"):
resource = resource[1:]
if resource == '': # todo default url, also resource can never be empty ('') cuz we deny it at verify_http_request
resource = DEFAULT_URL # todo check that i did correct
if resource in REDIRECTION_DICTIONARY:
headers = "http/1.1 302 Moved temporarily\r\n"
client_socket.send(headers.encode())
if not os.path.isfile(resource):
client_socket.send("http/1.1 404 Not Found\r\n".encode())
file_type = resource.split(".")[-1]
http_header = "http/1.1 200 OK\r\n"
if file_type == 'html':
http_header += "Content-Type: text/html; charset=utf-8\r\n"
elif file_type == 'jpg':
http_header += "Content-Type: text/jpeg\r\n"
elif file_type == 'js':
http_header += "Content-Type: text/javascript; charset=utf-8\r\n"
elif file_type == 'css':
http_header += "Content-Type: text/css\r\n"
# todo make sure if a file exists and he has permission but it's not that type what code to return?
data = get_file_data(resource)
http_response = f"{http_header}Content-Length {os.stat(resource).st_size}\r\n".encode() + f"{data}\r\n".encode()
client_socket.send(http_response)
There are several issues to consider:
r'GET js/jquery.min.js http/1.1\r\n'
which is a raw Python string literal, meaning backslashes become literal backslashes so \r\n
won't convert to the CRLF you want (carriage-return + linefeed). You may want to change the literal to not be prefixed with r
./js/jquery.min.js
instead (HTTP 1.1 RFC reference). Your server may ignore this but a conforming client would respect this.Host:
HTTP header. Your server doesn't require this and it's mostly important when hosting several domains on the same IP address, but it's good to know. In that case a request would look something likeGET /resource HTTP/1.1
Host: example.com
... more stuff here ...
curl -vvv
to see what's sent over the wire.% curl -vvv http://example.com
* Trying 93.184.216.34:80...
* Connected to example.com (93.184.216.34) port 80 (#0)
> GET / HTTP/1.1
> Host: example.com
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
... more stuff here ...