3

I have a couple questions about what is expected when it comes to the respond to client part of the code. My confusion arises when it comes to the part where we have to write out the bytes of the file. In the dynamic part of the code the variable size is referring to the size of the entire file (if I'm understanding correctly). The variable body points to the contents of the file in memory as loaded by load(). The code then calculates length which I believe is the size of the file minus the headers, thus describing the size of just the files bytes (the ones we have to write in the response). Then when the actual write comes, we get this:

write(cfd, body, size)

Doesn't this write the entire file in the response, including the headers? Is that what we want to do? I'm just not clear whether we're supposed to write the all the bytes in the file, or just the bytes after the header ie: the content. Is the only reason for the math involving the variables haystack, needle and size to extract the length for the "Content-Length" part of the response?

If anyone could offer some clarification on this I would appreciate it! As is stands I'm just going to experiment and assume that if my server is displaying the html and jpg pages then it's working as intended. Thanks in advance!

1
  • follow up note: When I try and send a response, I can see output in telnet that is identical to the staffs server program, but in the browser I get an ERR_CONTENT_LENGTH_MISMATCH error. The length output for Content-Length is the same for my program as the staff solution, and also matches the actual file size... Anyone encounter this error? It would appear that I'm very close to solving this one! – Andy Nonomous Aug 25 '15 at 3:36
1

Doesn't this write the entire file in the response, including the headers? Is that what we want to do?

yes, it does. and yes, this is what we want to do. mind you though that the headers are NOT those

HTTP/1.1 200 OK\r\n
Connection: close\r\n
Content-Length: <length>\r\n

where <length> is the length of the file contents. instead, as pointed out by the comment on the code, these are headers generated by php-cgi (the interpreter used to interpret the PHP code in the requested file). you may print the value of body at this point inside gdb to get an idea of what these headers are!

Is the only reason for the math involving the variables haystack, needle and size to extract the length for the "Content-Length" part of the response?

yes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .