tags:

views:

180

answers:

3

Hi,

I want to upload files from a client location to a server. At present, I have a client-server socket program by which I could send/receive files across, but I would like to improvise it.

My idea would be to transfer the file using HTTP PUT/POST from client (most of the coding on client side) to the server. Since I have no idea about HTTP programming, so I need some guidance on how to achieve that. I want to use C++ with BSD sockets in doing that, and no other libraries. My aim is to send the server a form, like as given below with a HTTP POST/PUT request, and get the file "main.cpp" uploaded to the server.

PUT http://localhost/ HTTP/1.0
Host: localhost
Content-type: form-data
Content-length: 90
FileUpload: /Users/SG/files/main.cpp

I was able to write a dummy program that does some PUT from a client, and the web server running Apache returns a HTTP 200. What I am failing to understand currently would be the following two things, which I guess are somewhat connected:

  1. How one could specify a file to be uploaded from the client in the form above?

  2. If I understand correctly, the file would be read at client site and then the file content would be sent to the server, where a server side script would read the bytes from client and create a copy of the file at the server. Is it correct?

Any help is appreciated.

Thanks,
Sayan

+4  A: 

As stated it's not possible. C++ alone has no sockets API. (Edit: with the addition of a BSD sockets API, it's now possible).

Your implementation might provide an OS-specific sockets API, in which case your question boils down to, "how do I write an HTTP client?". To which the answer is (a) don't, or (b) read the HTTP specification very carefully and do as it tells you. The basic steps are:

  1. (Possibly) parse a URL and use gethostbyname to get an IP address.
  2. Open a TCP socket, using connect.
  3. Write the request (you already have an idea what that looks like) using send.
  4. Read the response using read.
  5. Close the connection.

The difficult part is parsing the response robustly, because there are a lot of features to worry about. If your client is tied to a particular server, then its responses will be quite predictable, and you can ignore quite a lot of the HTTP spec (at least until you change the configuration of the server, update its software, etc).

If you're ready to give up before you finish, there are perfectly good HTTP libraries available, such as libcURL.

Steve Jessop
Isn't it possible even using HTTP POST? I could send a string containing the HTTP POST request (plus other info) to the server? I have used HTTP GET to download files from a server, so I guess there would be a way to upload using plain C++(?).
Sayan
You can't even *connect* to the HTTP server without a socket library. C++ itself has no socket library, so you need to use some OS-specific library to even get a socket.
Charles Salvia
Yes I get it now what you mean. I have modified my question.
Sayan
@Sayan: You can easily write a very simple HTTP POST client yes ... but it will be buggy. There are many edge cases. What do you do if the server is not HTTP 1.1 or if it returns a Redirect code or if there is an unexpected transparent proxy that does something odd. You will be spending many hours over the next years fixing small annoying things if you write your own.
Zan Lynx
Yes I agree that things could be messy in the future. But, I would still like to get a direction on that.
Sayan
Thanks Steve. I would follow that.
Sayan
+1  A: 

I want to use C++ for doing this, without the help of any libraries or system() calls.

Socket programming always requires system calls. C++ is an extremely general language. The ISO language definition does not specify anything about sockets or network programming, so in order to do this you need to rely on another system-specific library/standard, such as the BSD socket API on UNIX/Linux. Usually, in C++ you would use a higher-level third-party library that wraps these lower level system calls, such as Boost.ASIO. However, even that takes a bit of learning, to say nothing of correctly implementing the HTTP standard itself on top of your sockets.

Charles Salvia
Aha, I now understand what Steve meant when he said that there's no socket API for C++. Yes, I am using BSD sockets...so let me reframe my question accordingly. Thanks.
Sayan
A: 

Hi,

I was able to attain what I wanted. My http post request string looks like below:

"POST %s HTTP/1.0\r\n" 
"Host: %s\r\n"
"Content-type: multipart/form-data\r\n"
"Content-length: %d\r\n\r\n"
"Content-Disposition: %s; filename: %s\n"

I am sending the above string from a cpp program in client to the server, running Apache, which handles the data through a cgi script, also written in cpp. The data is placed with the Content Disposition attribute. Can anybody please point it out to me as to how would I transform the above to a HTTP PUT?

Thanks,
Sayan

Sayan