4

I am implementing a client and server in C (on linux) and i want to send a text file to a server from the client using an HTTP PUT message.

I'm not really sure how to do this. Do I first send the HTTP request and header line over the socket, and then send the file over the socket piece by piece using a buffer? or do I need to prepend every piece of the text file with its own HTTP request and header line before I send them over?

I also read about this function called sendfile that seems like it would make this easier, but I wasn't sure how I would prepend an HTTP header and request line to the file if sendfile just sends the file straight to the socket.

Thank you for your help.

3
  • Here is a minimal (but not entirely serious) example of doing HTTP over low-level sockets. Commented Nov 13, 2015 at 19:21
  • @AShelly there is nothing about PUT method there!!! Commented Nov 13, 2015 at 19:28
  • If you can send a GET, you can send a PUT. Commented Nov 13, 2015 at 19:29

2 Answers 2

7

Do I first send the HTTP request and header line over the socket, and then send the file over the socket piece by piece using a buffer?

Yes.

or do I need to prepend every piece of the text file with its own HTTP request and header line before I send them over?

No.

I also read about this function called sendfile that seems like it would make this easier, but I wasn't sure how I would prepend an HTTP header and request line to the file if sendfile just sends the file straight to the socket.

First send the header with a normal send, then call sendfile. However you should read the documentation, which says

Note that a successful call to sendfile() may write fewer bytes than requested; the caller should be prepared to retry the call if there were unsent bytes.

This means that your life is not that much easier with sendfile. It is more efficient than a combination of read and write, but the amount of work the programmer needs to do is similar in both cases.

Sign up to request clarification or add additional context in comments.

Comments

0

if you want to implement HTTP transmission protocol then check this RFC http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.2

else if you want your own easy method then you can for example implement an API such DWORD(cmdPut)+filename+'\0'+DWORD(fileLen)+fileBinaryData.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.