In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to understand the Request in the Http server written in C language. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Production process
Observe the http data received
Parsing the method url version of request
Parsing header
Parsing body
Observe the http data received
If you can complete a simple TCP/IP-based socket server program, then congratulations, you can read this article. HTTP is officially based on the application layer protocol of TCP/IP, so as long as our program can read HTTP data and respond in accordance with the HTTP protocol, then we can complete the communication of HTTP.
If you have a chance, you can use telnet to connect to our server, and you will get some meaningless characters. If it is a browser, what will it send? We tried to enter our server address: 127.0.0.1 virtual 9734 in the browser address bar and found that the browser said, "the response sent by 127.0.0.1 is invalid." That means that the data browsers we return to the browser cannot understand it, because modern browsers default to request access to our server using the http protocol, and the data we return is only a "helloworld" string and does not conform to the return format of the http protocol. Even so, browsers are sincere in sending standard http requests to our servers. Let's take a look at the messages received by our servers:
If you look at it for a while, it looks like the * * line is the type of http request, and the second line starts with some key-value pairs separated by the ":" sign. Indeed, the * line tells us that we are using the GET request, the url of the request is "/", and the HTTP version 1.1 is used. The second line begins with the request header for HTTP. In addition to GET requests, another common request is POST. If it is a little troublesome to send a POST request from a browser, we will use the curl tool to send a HTTP POST request to the server to see what the data will look like: curl-d "message=nice to meet you" 127.0.0.1:9734/hello
The message received by the server:
You can see that the header information is followed by a blank line and the body data information of the POST. Also notice the Content-Length header, which represents the size of POST's body data.
Parsing the method url version of request
Let's start by parsing the simplest * * line: "POST / hell HTTP/1.1". You just need to split three strings with a space.
1.request.h
2.request.c
3. Write test cases
Execute: `gcc.. / request.h.. / request.c requestTest.c & &. / a.out` under the test directory, and you can see that our parsing method is correct.
Parsing header
The parsing of header looks complicated, and it's easy to see that each line is a split key-value pair with ":", so we can express it in HashMap. How to judge the end of header data? through the previous observation, we can find that if it is POST, there will be a blank line separated from body, and if it is GET, we can only check whether the data of the client has been sent, which means that the header has ended. Before formally parsing the header, let's construct the data structure of the basic data for later use.
1. Create a linked list structure
two。 Create a hash table structure
3. Parse header by line and stop at the end of a blank line or string
Because there is a lot of code, the implementation code of the two structures will not be posted here soon, and you can trust me to get the "code" if you need it.
Parsing header code, with a hash structure, parsing header is much more convenient, as long as it is split into key and value according to ":"
Key code:
Parsing body
Parsing body is very simple. If * a line is not a blank space or a blank line, there is body data, and the blank line is followed by body data.
There is a key key in the header, 'Content-Length' represents how long the body is, and we can use this field to determine the end of the body.
It's done. Print our results.
Print parsed content
Execute gcc request.h request.c main.c tools/utils.c tools/utils.h & &. / a.out
Then open a new terminal to execute curl-d "message=nice to meet you" 127.0.0.1:9734/hello-everyone
See the output:
On how to understand the C language to write Http server Request to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.