In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
In this issue, the editor will bring you about how to achieve a Http server. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
When it comes to http protocols and http requests, many people know, but do they really "know"? I have interviewed a lot of job seekers, and they can talk a lot about the http agreement, and then I ask him what the format of the http agreement looks like. Many people don't know, if they don't know, he can even pull the header of the http protocol to the header of the html document. When I ask http GET and POST requests, most people can answer the form of GET requests, but many people are confused about where the data requested by POST is put and how the server recognizes and parses the POST data. When it comes to http servers, many people have no way to implement a http server without ready-made http server such as apache and Nginx. If some simple http requests are needed in the actual application scenario, it is a lot of work to use heavy-duty http server programs such as apache and Nginx. You can try to implement a simple one yourself.
If you can't answer the questions mentioned above clearly, you can read this article, which not only introduces the format of http, but also leads you to implement a simple http server program from scratch.
Introduction of http Protocol
1. Http protocol is an application layer protocol, which is generally based on tcp protocol (of course, your implementation must be based on udp), that is to say, the data of http protocol is sent and received through tcp protocol.
2. Http protocol is also divided into two parts: head and body, but the and tags in html are not the head and body of http protocol, they are all body parts of http protocol.
So what does the head of the http protocol look like? Let's introduce the http protocol.
The format of http protocol is as follows:
The url path of 1GET or POST request (usually the path to remove the domain name) HTTP protocol version number\ r\ n2 Field 1 name: field 1 value\ r\ n3 Field 2 name: field 2 value\ r\ n4 … 5 Field n name: field n value\ r\ n 6\ r\ n 7http protocol packet content
In other words, the http protocol consists of two parts: the header and the packet body. A\ r\ nSegment is used between the header and the packet body. Since every line of the http protocol header ends with\ r\ n, the http protocol header generally ends with\ r\ n\ r\ n.
For example, we request the URL http://www.hootina.org/index_2013.php in the browser, which is a typical GET method. The format of the http packet assembled by the browser is as follows:
GET / index_2013.php HTTP/1.1\ r\ n2Host: www.hootina.org\ r\ n3Connection: keep-alive\ r\ n4Upgrade-Insecure-Requests: 1\ r\ n5User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36\ r\ n6Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/* Qroom0.8\ r\ n7Accept-Encoding: gzip, deflate\ r\ n8Accept-Language: zh-CN,zh;q=0.9,en;q=0.8\ r\ n9\ r\ n
In the above request, only the header has no packet body, and the packet body of the http protocol is not required, which means that the GET request generally does not have a packet body.
If a GET request takes parameters, it is usually appended to the url of the request, and the parameters are separated by & split, for example, the request http://www.hootina.org/index_2013.php?param1=value1 m2=value2 m3=value3. Let's take a look at the format of the http protocol package assembled by this request:
GET / index_2013.php?param1=value1¶m2=value2¶m3=value3 HTTP/1.1\ r\ n2Host: www.hootina.org\ r\ n3Connection: keep-alive\ r\ n4Upgrade-Insecure-Requests: 1\ r\ n5User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36\ r\ n6Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/* Qroom0.8\ r\ n7Accept-Encoding: gzip, deflate\ r\ n8Accept-Language: zh-CN,zh;q=0.9,en;q=0.8\ r\ n9\ r\ n
By contrast, you now know where the GET parameter of the http protocol is placed in the protocol package.
So where does POST's data go? We log in to the 12306 website https://kyfw.12306.cn/otn/login/init and enter the user name and password:
Then we found that the browser assembled the http protocol package in POST and sent our user name, password and other information. The format of the assembled package is as follows:
POST / passport/web/login HTTP/1.1\ r\ n2Host: kyfw.12306.cn\ r\ n3Connection: keep-alive\ r\ n4Content-Length: 55\ r\ n5Accept: application/json, text/javascript, * / *; qroom0.01\ r\ n6Origin: https://kyfw.12306.cn\r\n 7X-Requested-With: XMLHttpRequest\ r\ n8User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64 X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36\ r\ n9Content-Type: application/x-www-form-urlencoded; charset=UTF-8\ r\ n10Referer: https://kyfw.12306.cn/otn/login/init\r\n 11Accept-Encoding: gzip, deflate, br\ r\ n12Accept-Language: zh-CN,zh;q=0.9,en;q=0.8\ r\ n13Cookie: _ passport_session=0b2cc5b86eb74bcc976bfa9dfef3e8a20712; _ passport_ct=18d19b0930954d76b8057c732ce4cdcat8137; route=6f50b51faa11b987e576cdb301e545c4; RAIL_EXPIRATION=1526718782244 RAIL_DEVICEID=QuRAhOyIWv9lwWEhkq03x5Yl_livKZxx7gW6_-52oTZQda1c4zmVWxdw5Zk79xSDFHe9LJ57F8luYOFp_yahxDXQAOmEV8U1VgXavacuM2UPCFy3knfn42yTsJM3EYOy-hwpsP-jTb2OXevJj5acf40XsvsPDcM7; BIGipServerpool_passport=300745226.50215.0000; BIGipServerotn=1257243146.38945.0000; BIGipServerpassport=1005060362.50215.0000\ r\ n 14\ r\ n15username=balloonwj%40qq.com&password=iloveyou&appid=otn
Username=balloonwj%40qq.com&password=iloveyou&appid=otn is our POST data, but you need to pay attention to the following, don't get it wrong:
1. My user name is balloonwj@qq.com, which becomes balloonwj%40qq.com in POST, where% 40 is the hexadecimal transcoding form of the @ symbol. You can refer to this code table here: http://www.w3school.com.cn/tags/html_ref_urlencode.html
two。 There are three variables, username, password, and appid, separated by the & symbol, but note that this does not mean that the & symbol must be used when passing multiple POST variables, but this is just the default way for browser html forms (text boxes for user names and passwords are one of the html forms) to split multiple variables. You can customize it according to your needs, just let the server know how you parse. For example, it can be divided like this:
Method one
Username=balloonwj%40qq.com | password=iloveyou | appid=otn
Method two
Username:balloonwj%40qq.com\ r\ n2password:iloveyou\ r\ n3appid:otn\ r\ n
Method three
Username,password,appid=balloonwj%40qq.com,iloveyou,otn
No matter how you divide it, as long as you can analyze it according to certain rules.
I don't know if you have noticed, but the above POST data is placed in the http package. How does the server parse it? Maybe you don't understand what I mean. Take a look at the following picture:
As shown in the figure above, since the http protocol is based on the tcp protocol and the tcp protocol is a streaming protocol, the packet header can be delimited by the extra\ r\ n.How can the packet body be demarcated? This is a problem to be solved by the agreement itself. At present, there are generally two ways. The first way is to have a content-Length field in the packet header. The value of this field identifies the length of the POST data. 55 in the figure above is the length of the data username=balloonwj%40qq.com&password=iloveyou&appid=otn. After receiving a packet, the server first parses the value of this field from the packet header, and then reads the packet data of the corresponding length as the http protocol. There is also a format called http chunked (chunking), which roughly means dividing large packets into small packets, and readers who are interested in the details can search and learn on their own.
Http client implementation
If you can master the http protocol mentioned above, you can send http requests yourself through the code assembly http protocol (which is also the practice of various open source http libraries). Let's start with a brief introduction to how to simulate sending http. For example, if we want to request http://www.hootina.org/index_2013.php, we can first get the ip address through the domain name, that is, the ip address of www.hootina.org through socket API gethostbyname (). Since the default port number of the http server is 80, after we have the domain name and ip address, we use socket API connect () to connect to the server, and then assemble the http protocol package according to the format described above. Using the socket API send () function to send out, if the server has a reply, we can use socket API recv () to accept the data, followed by parsing the data (parsing the header and the packet body first).
Http server implementation
Let's simplify some problems here. Let's assume that the requests sent by the client are all GET requests. When the client sends the http request, we get the http package and deal with it accordingly. We thought that our flamingo server implemented a registration request that supports http format as an example. Suppose the user enters the following URL in the browser to achieve a registration function:
Http://120.55.94.78:12345/register.do?p={"username": "13917043329", "nickname": "balloon", "password": "123"}
Here our http protocol uses the port number 12345 instead of the default port 80. How to listen to port 12345, this is a very basic knowledge, I will not introduce it here. When we received the data:
1void HttpSession::OnRead (const std::shared_ptr& conn, Buffer* pBuffer, Timestamp receivTime) 2 {3 / / LOG_INFO peek (), pBuffer- > readableBytes (); 8 / / because the data of a http header is at least\ r\ n\ r\ n, so it is greater than 4 characters 9 / / less than or equal to 4 characters, indicating that the data has not been collected, quit, and wait for the bottom layer of the network to charge 10 if (inbuf.length () forceClose () 35 return; 36} 37 38 std::vector chunk; 39 StringUtil::Split (lines [0], chunk, ""); 40 / / chunk contains at least three strings: GET+url+HTTP version number 41 if (chunk.size ())
< 3) 42 { 43 conn->ForceClose (); 44 return; 45} 46 47 LOG_INFO forceClose (); return;} LOG_INFO
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.