First, Introduction
We all know that Web programming protocol is the HTTP protocol, called Hypertext Transfer Protocol. In the Java EE we can quickly implement a Web project, but in C + + is not very fast, the reason is that the bottom of the socket network writing needs to do its own, the upper layer of the HTTP protocol needs to be done by ourselves, the user interface needs to be done by ourselves, How to be efficient and design a framework is a very difficult thing. But these things Java is already in the bottom of the package for us, and we are just doing business layer of things.
In this HTTP server implementation, the use of C + + libraries and socket sockets for programming and pthread thread writing. Deny use of third-party libraries. Because the main thing is to let everyone know the basic way of implementation, to remove some features such as security, efficiency, but anyway, the rationale of the third-party business library is consistent, but they are optimized. At the beginning of the writing, I will not be all about the content of the HTTP protocol, this is too boring, I just explain some of the following protocol fields need to use.
In writing this article, there are some confusion before, C + + in the end what can do, to the online search, nothing but to develop games, embedded programming, write servers and so on. Then if asked how to write a server, then these network water people will tell you, you first learn the basics, see what books, then you know, I can only hehe, in the study of no purpose, although you do not know how to write, because although you know some general, but no one leads you to get started, We still can't write a thing we want, I write this blog is mainly a small stepping stone, although there are many blogs online, about how to write an HTTP server, but not a third-party library ACL, or just a few lines of code, or to join some Microsoft C # Content or MFC, which in my opinion is just something insignificant, after joining perhaps you are comfortable on the interface, but greatly increased our learning costs, because these interface code changes the process we know the flow of the program, there are some interface code and core code mix, very bad for learning.
Second, the HTTP protocol
When you enter http://10.1.18.4/doing on the URL entry field in the browser. The browser sends the following protocol header to the 10.1.18.4 server 80 port process, which is a text string. Each line ends with \ r \ n. Represents a carriage return line break.
1 get/doing http/1.12 host:10.1.18.43 user-agent:mozilla/5.0 (Windows NT 6.2; rv:40.0) gecko/20100101 Firefox/40.04 Acc ept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.85 accept-language:zh-cn,zh;q=0.8,en-us;q=0.5 , en;q=0.36 accept-encoding:gzip, Deflate7 referer:http://10.1.18.4/8 connection:keep-alive
So know that we actually sent a URL request that was actually converted to a string like the one above. Here I simply explain what this protocol header means because you can find a lot of information on the web to explain them.
1) The first line of get/doing HTTP1.1 means that the request is Get,url is/doing, the version of the HTTP protocol is 1.1
2) The Host in the second row is the IP of the server
3) The third line of User-agent represents what browser you are using to run on what system. From the above can be this message is displayed on the window of the Firefox browser issued by the request header
4) On line fourth, accept represents the format of the information that the browser can accept, either text, HTML, or application files (binary files). where q represents the weight, the more willing to accept the previous information. There are some other content that readers can own Baidu.
5) In some of the following information, there is nothing to use, I do not explain, see the text meaning also probably know some information. Please search the network for details.
The most important thing is when a request comes to an end, that is, a blank line represents the end. is actually the "\ r \ n" End.
Say so many people may still be a little confused, know these so in the program and how to achieve it. I was confused, and now I propose one of the simplest implementations is to directly connect a string. I decomposed it in the actual implementation, but now I'm explaining to write the program as follows:
1 char *str= "get/doing http/1.1\r\n2 host:10.1.18.4\r\n3 user-agent:mozilla/5.0 (Windows NT 6.2; rv:40.0) gecko/20100101 firefox/40.0\r\n4 accept:text/html,application/xhtml+xml,application/xml;q=0.9,*//*;q=0.8 \r\n5 accept-language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n6 Accept-encoding:gzip, Deflate\r\n7 referer:http:// 10.1.18.4/\r\n8 connection:keep-alive\r\n9 range:bytes=14584264-\r\n\r\n ";
Maybe the above protocol is a bit different from the previous one, it's OK, I just intercepted some content to enter. Very simple is the C language of the char* string. At the end of the line there is a ' \ ', which means that the input line, minus the line, you need to write the contents of the device to a line, C language grammar, do not understand the reader can consult the C language of the string. What I want to say is that at the end of each line there is a \ r \ n. These two escape characters represent carriage return line breaks. And in line 9th there are 2 \ r \ n, the last one represents a blank line, meaning to tell the server my protocol header to this location.
Why you need a blank line, here is a little information on network programming. In socket TCP stream programming, such as when you call the Write or read function, the internal is not a one-time accept or send all information. So when we send the above str, not necessarily all at once, then the server does not know when the end. So we need the HTTP rule to end with a blank line as the end of the protocol header.
Next up is the server we wrote to accept this string. The entire protocol header is accepted as a blank line and then parsed. The following is the code that parses this string, which I encapsulate in the project, but now we just need to know how to implement the parsing function.
1 #include <iostream> 2 #include <cstring> 3 #include <vector> 4 #include <map> 5 #include <al Gorithm> 6 using namespace std; 7 8 char *str= "get/download/jbpm4s.tt http/1.1\r\n 9 host:10.1.18.4\r\n10 user-agent:mozilla/5.0 (Windows NT 6.2; rv:40.0) gecko/20100101 firefox/40.0\r\n11 accept:text/html,application/xhtml+xml,application/xml;q=0.9,*//*;q= 0.8\r\n12 accept-language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n13 Accept-encoding:gzip, deflate\r\n14 Referer: Http:http://10.1.18.4/\r\n15 Connection:keep-alive\r\n16 range:bytes=14584264-\r\n\r\n "; string& LTrim ( String &str) {String::iterator p = find_if (Str.begin (), Str.end (), Not1 (Ptr_fun<int, int> (isspace))); Str.erase (Str.begin (), p); return str; string& RTrim (String &str) {String::reverse_iterator p = find_if (Str.rbegin (), Str.rend (), NOT1 (Ptr_fun<int, int> (isspace))); Str.erase (P.base (), Str.end ());return str; string& Trim (string &str) {LTrim (RTrim (str)); return to STR; GetContent string (string& str,int Start,char c,int &pos) {int i=start;36 int len=str.size (); 37 while (i<len&&str[i]!=c) {i++;39}40 pos=i;41 return str.substr (Start,i-start); 42}43 map<string,string> Parseheader (char* str) {Len=strlen (str), vector<string> vs;46 int i=0;4 7 while (I<len) {str[i]!= ' \ r ') {str[i]!= int j=i;50 while (i<len&& \ r ') Wuyi i++;52 vs.push_back (String (str+j,str+i));}else{54 i+=2;55 }56}57 int pos;58 string method=getcontent (vs[0],0, ", POS); ) +1, ", POS); map<string,string> mp;61 mp[" Method "]=method;62 mp[" Url "]=url;63 for (int I=1;i<vs . Size (); i++) {StriNg key=getcontent (vs[i],0, ': ', pos); Value=vs[i].substr string (pos+1); Mp[key]=trim (value); 67}68 return mp;69}70 int main (int argc, char **argv), {map<string,string> MP =parseheader (str); Map<string,string>::const_iterator It=mp.begin (); It!=mp.end (); ++it) {Cout<<it->first << "<<it->second<<endl;76}77 return 0;78}
Put some information into a map. The parsing here is to process each row first, and then parse each row. Maybe this kind of processing is a bit slow, but it doesn't matter, because the string is relatively short anyway, in large concurrency under the efficiency will not affect too much, if you have any better analytic way, you can reply to me.
After parsing the header information on the server, we can get/doing this URL, so that our service can be the client needs to return the content to the client, here is the content of the browser request is legitimate whether the existence of this information. Should be described in the corresponding response header, in the "HTTP Server implementation file upload and download (ii)" will be described.
You are welcome to discuss these issues together. Have any idea of the person to reply to me, we study together, progress together oh.
HTTP server implementation file upload and download (a)