HTTP/1.1 200 OK doesn't work? - c

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.

Related

http post request to host with https url in C language returns HTTP 302

i have program in c in which i am trying to post data through http request with host having url as https then in response HTTP 302 status of redirection is coming always, but for host with url on http (just for checking)it doesn't show redirection related status.
HTTP and HTTPS may have absolutely different behaviour, they may lead to different content, they are even run on different TCP ports (80 and 443 by default). So, you should not compare HTTP and HTTPS. Also it is very common when http redirects you to https (it's not your case, but it might be something else like that).
So, just implement redirection.

How to parse HTTP requests from a C based web server

I have a programming project where I have to create a multithreaded web server which handles HTTP requests.
I just learned socket programming and I got a client and a server running. I wanted to know what the best method would be for parsing the HTTP request headers. I saw this: how to parse http request in c++ a few minutes back. But I would rather not shift to C++ at this point. So how should one go about parsing an HTTP request in C?
You can have a look at the web servers in C such as mongoose (https://github.com/cesanta/mongoose/blob/master/mongoose.c) and could use the same methodology to parse the http request. But what would I suggest is that just go through the HTTP RFC 2616 since that would help you in writing your own parser for http requests.
By the way which kind of HTTP requests your Server is handling (GET or POST or BOTH) ??
In http post requests the HTTP header & data are separated by "\r\n\r\n".
In received data sscanf the Content-Length form the http header then start reading the data after you get "\r\n\r\n" until you get the same amount of data as mentioned in Content-Length.

how http server received file from client

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";
`

how to get http status code in a web client written in c

I am currently building an http web client using sockets which gets a url as input and should perform the following task: Connect to the web server and save the response of the server in a file, and print the status code included in the http response.
I have completed the first part of writing in a file and I am having a problem with retrieving the status code. Is there a library or a function that could help me?
You should use libcurl to perform the HTTP request.
And the function curl_easy_getinfo to get the HTTP code.
The first line of the response is the status line, and is very easy to parse. It consists of the protocol version followed by a numeric status code and its associated textual phrase ("OK", "Not Found" etc).
For example:
HTTP/1.1 200 OK
The exact syntax and the list of valid codes is documented in RFC 2616 (section 6.1).

C webserver header sending

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.

Resources