I have written code for sending client user passwords to an HTTP server for verification via HTTP. I generate the query string (containing usr, pwd) and send the request to the server. That works.
But now I have to send a file (text/xml) to that server. I don't know how it can be done.
Do we have to write some code on the server or only in the client?
What are the mechanisms on the server for accepting file and on the client for sending files?
The HTTP protocol is really simple, actually:
the client sends a line containing HTTP method name, URL and protocol version
the client sends an RFC822 header containing request parameters and, if a data block follows, details about the data block.
the client sends the data block
the server sends a line containing protocol version, status code and message
the server sends an RFC822 header containing response parameters and, if a data block follows (or the client performed a HEAD request), details about the data block
the server sends the data block, unless the method is HEAD.
the connection is either torn down, or the protocol restarted.
Typically, servers will understand at least these methods:
GET (client does not send data block, server sends data block)
HEAD (same as GET, but server omits response data block)
POST (client sends data block, server responds with data block)
PUT (client sends data block, server does not send data block)
There is some implied semantics in the choice of method, in that GET requests never modify server state and their results may be cached and reused (which is what allows the browser to go back and forth between pages), while POST requests do change server state -- incidentally, this is what you do when you upload a file.
So, in order to send a file, prepare a POST or PUT request (depending on whether you expect a reply document, or if a simple acknowledgement status code is sufficient), which consists of the request line, the headers containing extra protocol info ("Host:", "User-Agent:", ...), the headers describing the file ("Content-Type:", "Content-Length:", ...), an empty line, and the file contents, and send that over a TCP connection, then read back the status line, the response headers and the response file (if you asked for one).
It depends on the server's application how to load the file.
You may need to send the file using the HTTP "POST" method instead of "GET".
POST /your_uri HTTP/1.1
Host: www.yourhost.com
Content-type: application/x-www-form-urlencoded
Content-length: 41
filename=test.xml&data=yoururlencodeddata
The server application may expect files encoded with "multipart/form-data" boundaries, something like that:
Content-type: multipart/form-data, boundary=AaBb01x
--AaBb01x
content-disposition: form-data; name="yourfield"
Your field data
--AaBb01x
content-disposition: form-data; name="yourfilefield"; filename="filename.xml"
Content-Type: text/xml
<root>your xml data</root>
--AaBb01x
If its a XML file it is easy.
You can add Content-Type: text/xml in the HTTP header and append the XML file data after the \r\n\r\n of the HTTP header and send it via the socket to the webserver.
The webserver will understand from the HTTP header that it contains XML file and takes the file. In the case of a bnary file, you will need to convert it to base64.
For example I have used a buffer to store the http request. Now if you send this buffer to the socket connected to the webserver, the FileName.xml will be saved in the webserver. For this to work the upload.php has to able to work with POST data.
The boundary is to show the boundary between the data and is needed by the HTTP protocol. It can be any random generated number and make sure the start boundary and the close boundary numbers are equal. Also content length is the length of the file.
`
char buf[2048] = "POST http://www.nameofyoursite.com/upload.php HTTP/1.1\r\n"
"Host: www.nameofyoursite.com\r\n"
"Content-Type: multipart-form-data, boundary=1234567\r\n"
"Content-Length: 15\r\n\r\n"
"--1234567\r\n"
"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"FileName.xml\"\r\n"
"Content-Type: text/xml\r\n"
"<xml>This is a test</xml>\r\n"
"--1234567--\r\n";
`
Related
I am looking for a standard way of sending a POST request with a single file in it, as it contains a single file we are looking for something else than multipart/form-data,
We thought about using the Content-Disposition header or the Content-Type, the first one looks like it is only valid for a multipart request or as a response header (not request), not sure if only using the Content-Type to a file type would be enough.
Hi I'm a university student and I'm doing a server that writes on a browser. I'm using C language.
I have to write some messages on the browser, I was able to print "HTTP/ 1.1 200 OK" but when I try to print "HTTP/1.1 200 OK" nothing appears in the browser window. I doesn't work.
I must be doing some kind of confusing here. I searched on this site for answers and I saw some people suggesting to use "HTTP/1.1 200 OK\r\n" this also does not work.
I have this sentence in a buffer and I write this directly in the socket connected to the browser with the function write.
write(newsockfd, aux_buffer, tamanho)
Most web applications do not write HTTP directly but use some sort of framework that handles the HTTP communication. However, if you do are interested in writing HTTP directly, you should learn how HTTP works first. For example, a minimal HTTP response might look like this:
HTTP/1.1 200 OK
Content-Type: text/html
<html><head><title>Hello World</title></head><h1>Hello World</h1></html>
Notable here is the HTTP status line and HTTP Headers. The HTTP status line and HTTP Headers are handled by the browser and are normally not shown to the user; the Content-Type header tells the browser that the response body is of MIME text/html (not text/plain or application/xml or image/jpeg or ...), so the browser should use its HTML rendering engine to display the body. There are many HTTP Headers defined by HTTP Specfication (RFC 2616) related to content negotiation, auth, encoding, caching, expiry, redirection, proxying, and many more. The header fields be followed by two newlines, which marks the end of headers fields, and the start of the response body.
Note that for strict conformance to the HTTP spec, the newlines that separates the headers must be \r\n (CR+LF), although HTTP spec recommends (but does not require) that HTTP agents should also recognize other newline styles. Also, the HTTP header must only contain printable US-ASCII characters and may only contain characters from other charset if encoded using RFC 2047. This rule about newline style and US-ASCII encoding only applies to the HTTP header, the response body are free to use other newline styles and encodings.
When a user selected a file and click submit, all the other input fields are transferred in HTTP header. But what happened to the file? How did it get transferred to the server? What protocol has been used?
When you POST data to a server, said data will be transmitted in the HTTP body rather than the HTTP header. The file will be encoded as either application/x-www-form-urlencoded or multipart/form-data (as described in the HTML 4.01 specification).
Question
I have implemented web server in C. But web server often receives sigpipe signal and dies. I'd like to understand why sigpipe error occurs. I have just learned that sigpipe is said to occur if client closes socket earlier than server. But I think there is not possibility that client close socket earlier, since my server do not sends additional header information. for example, cache information, date information, etc.
So, I'm curious if there is any case that web browser has no interest in my poor web server-response. And how client can close socket earlier than server?
Some scenario
my server wait for request message for client. it then parse only status line.
it then send response message to browser(client).
if the requested file is HTML document, server sends the following response message. That is, server writes HTML text into socket, not existing file on hard disk.
HTTP/1.1 200 OK
Connection: close
<html file>
if the requested file is *.jpg, *.gif, or *.ico, server sends the following message.
HTTP/1.1 200 OK
Connection: close
Accept-Ranges: bytes
Content-length: <file size>
Content-Type:image/jpg, gif or x-icon
<some binary information for image>
Any help would be awesome. Thank you.
I wrote a simple web server in C that listens for a connection and send some text over a socket.
I want my server to be accessed by a server. In this case I should s*end headers such as CODE(200), Content type, Content length*.
How these headers can be sent to the client(browser or telnet)?
How headers of request can be extracted?
(Maybe, I just don't understand the question, vote me down.)
The http response header format is not too difficult:
header1\n
header2\n
<empty line>\n
content\n
content\n
That's all. The webserver should send the headers, an empty line, then the content.
If you wanna test header sending, you should check it with a browser. Add a line to your webserver to send the following header:
Content-Disposition: attachment; filename=download_me.txt
So when you connect to your webserver with a browser, it will pop up a "save as" dialog instead of displaying the downloaded web page in the browser window. If you got the dialog, and the downloaded file is also OK, your webserver sends the headers properly.
G
It's another issue, wether the client handles them as you (and W3C) expect...
Edit: HTTP ok status is:
HTTP/1.1 200 OK\n
Google for more status codes.