First, Introduction
Welcome to see my blog, how everyone has any ideas to reply to my Oh, gossip not much chat, and then on the content to say, in the previous section has already talked about the request header string parsing, and in the parsing I we have obtained the URL. Is the/doing mentioned in the previous section. When the browser sends the/doing request, this is the connection to the server and there is no interruption, the reason is that the browser waits to receive the information sent by the service, then he parses the information and displays it in the browser interface. When writing the HTTP server's C/s structure, I only write service-side server, the client directly with the ready-made browser.
When the server finds what the browser needs, the server now needs to send the content to the browser. However, in the HTTP protocol, before sending the specific content, the server is still to send a response header, before the browser is sent called the request header, then the server will also send a response header to tell the browser, said your request I accept or reject. No specific entity content is sent after the rejection, otherwise tell the browser that the format of the content you requested is HTML or text, or binary files such as audio, and tells the browser that you requested the size of the content (this is very important). When the response hair is finished, if you agree to the browser request, then send the specific content, and then close the link.
Second, the HTTP protocol
Now let's take a look at an example of this response header, or just some of the things we need to know now, and other content readers can find the relevant content on their own.
1 http/1.1 OK2 Server: (Unix) 3 content-type:application/octet-stream4 content-length:651443695 Connection: Keep-alive
In this example the response header we can see:
1) in the first line tells the browser HTTP protocol version is 1.1, the status code is 200, meaning OK, that is, your request I agree.
2) in the second line is to tell the browser the name of my server.
3) in the third line is the format of the content you requested is applicaiton/octet-stream, which is understood as a binary stream.
4) In line fourth, indicate the size of the content you requested, in bytes.
5) Line fifth tells the browser that the keep-alive is connected.
In this protocol, when the hair is sent to the browser, there is a return line at the end of each line \ r \ n. Finally, you need a blank line to represent the end of this response header. There are also some of the contents of the agreement to use the time to talk about, now put forward these items can be. When this protocol hair is sent to the browser, the browser parses it. Then wait for the real request content. This connection is closed when the server sends the details of the browser's request.
This is where the basic one request response is over. Let's talk about how to deal with big concurrency here. In fact, in the HTTP request, in each URL request the server does not care about whether the request is the same browser, the server does just have the request to send data to its connection, and then close the data. Although it is possible that the request on the same client has a data relationship, the server is only sending the data. So many requests from the same browser are treated the same as requests sent by different browsers or different hosts. Is that each request is independent and does not concern itself with this logical relationship.
In this article, the main content is finished, it seems to be relatively short oh, haha ... So, let's talk about some details.
Now the large concurrent server Linux, mainly in the thread, process, Select,poll,epoll way. These methods are available on every blog, so I don't want to explain that readers can look at the differences themselves. The HTTP server developed by this article takes the form of threading. Basically, when a request comes in, a thread is created to handle it. Uses the Linux under POSIX standard pthtread thread. If other readers can deal with it in several other ways. Or you can write a thread pool instead of one request at a time to create one. It is primarily wrapped in a thread class with a namespace of multithread, followed by some code snippets:
Header file (include/thread.h):
1/* 2 * Include/thread.h 3 */ 4 5 #ifndef thread_h_ 6 #define THREAD_H_ 7 #include <pthread.h> 8 #includ E<iostream> 9 namespace Multithread {Ten-class thread {public:13 thread (), ~thread (); int Create_thread (pthread_t &pid,void* (*pfunc) (void *), void *arg), + int stop_thread (); +};19//* NAMESPA Ce multithread */21 #endif/* thread_h_ */
CPP file (src/thread.cpp)
1/** 2 * 3 * src/thread.cpp 4 **/5 #include "thread.h" 6 namespace multithread {7 8 Thread::thread () {9}10 11 Thread::~thread () {}13 int thread::create_thread (pthread_t &pid,void* (*pfunc) (void *), void *arg) { int r Et=pthread_create (&pid,null,pfunc,arg); if (ret==0) std::cout<< "Create_thread () ... succeed "<<std::endl;18 else19 std::cout<<" Create_thread () ... failed "<<std::endl;20 return ret;21}22 int thread::stop_thread () { 0;25}26//* Namespace multithread */
See now, we must find Stop_thread () is empty, should be for me to now still useless this function. And now only use the Create_thread function, and then if you use it, then put it in a later article.
If you are interested in HTTP server, then see "HTTP Server implementation file upload and download (iii)" and I learn HTTP server write it.
HTTP server implementation file upload and download (ii)