Server Program Analysis Based on HTTP Protocol Min CHE1 Ming Fu TUO1 1College of Science, Air Force Engineering University 710051 Xi’An, China
Total Page:16
File Type:pdf, Size:1020Kb
MATEC Web of Conferences 63, 05023 (2016) DOI: 10.1051/matecconf/20166305023 MMME 2016 Server Program Analysis Based on HTTP Protocol Min CHE1 Ming Fu TUO1 1College of Science, Air Force Engineering University 710051 Xi’an, China Abstract—The overwhelming majority of Web developments are built based on Web application of HTTP protocol. To analyze the server program based on the HTTP protocol and the process of interaction between the browser and server, a simple HTTP-based server program is taken as an example to describe how to use the theories of multi- threading and asynchronous operationˈand the core section of HTTP-based server program is gradually resolved with Winsock programming. This method can achieve the purpose to avoid blocking the calling threadˈand improve the responsiveness of a server program. Keywords- HTTP; HTTP protocol; Server; WinSock 1 INTRODUCTION 2.2. HTTP response consists of three components namely the status line,message header,and response body . Hypertext Transfer Protocol (HTTP) is the protocol of web’s application layer. It is the web's core issue. HTTP can be The format of status line is: HTTP-Version Status-Code Rea- realized in the web client’s program or server’s program. The son-Phrase CRLF. HTTP-Version indicates the version of programs running on different end systems of client and server HTTP protocol used in web server; Status-Code indicates the can exchange with each other through exchanging HTTP status code of a response sent back from web server; Reason- message. Web Page is also called document composed of Phrase indicates the text description of status code. The status various objects. Object is a document which is only single code is made up of three digits. The first digit defines the URL addressable. It might be an HTML file, or JPG image, or categories and there are five possible values: GIF image, or JAVA applets, or voice clip, etc. Usually, most 1xx: Indication Information It indicates that the request web pages consist of one basic HIML file and a number of the has been received and processing was continued; referenced objects. HTTP protocol defines how to request a web page from the client (web browser) and how to transfer the 2xx: Success It indicates that the request has been web pages to client from web server. successfully received, understood, and accepted; 3xx: Redirection It indicates that further operating is 2 HTTP PROTOCOL needed to complete the request; HTTP is a protocol of application layer basing on stateless, 4xx: Client Error It indicates that the request has a syntax request and response model. Usually it is based on TCP error or cannot be achieved; connections. The vast majority of web developers are the web applications on the basis of HTTP protocol. A lightweight web 5xx: Server Error It indicates that the server failed to server is accomplished via the program in this study [1]. achieve a legitimate request 2.1. HTTP request consists of three parts namely consumer Common status code, status description, explanation:[3] request, line interest header, and request body. 200 OK //successful client requests Request line begins with a method symbol separated by a 400 Bad Request//Syntax error in the client request. It space, then followed by the Request-URI and the version of HTTP. The form is defined as follows: Method Request-URI cannot be understood by the server HTTP-Version CRLF. Method represents request method; HTTP message is composed of requests from the client to the Request-URI is a uniform resource identifier; HTTP-Version server and responses from server to client. Request and indicates the request version of HTTP protocol; CRLF response messages consist of start line (For request message, represents carriage return and line feed. Except the CRLF at the the start line is the request line; For response message, the start end, CRLF cannot be separated into CR or LF characters in line is the status line), message header (optional), blank lines other part [2]. (only CRLF line), and message body (optional). © The Authors, published by EDP Sciences. This is an open access article distributed under the terms of the Creative Commons Attribution License 4.0 (http://creativecommons.org/licenses/by/4.0/). MATEC Web of Conferences 63, 05023 (2016) DOI: 10.1051/matecconf/20166305023 MMME 2016 3 HTTPSVR PROGRAM ANALYSIS HTTPSVR is a simple web server with a graphical interface, you can specify the port and the main directory of web page.You can generate web accessing log. It is constituted by an endless loop: Receive the request from client, parse and process the request according to the HTTP protocol, then sent the response back to the client. 3.1. HTTPSVR main work flow chart HTTPSVR is a powerful software with rich graphical interface. The main workflow is shown in Figure 1. Firstly ClistenSocket: OnAccept (int nError-Code) function generates CRequestSocket class. When monitoring the connection requests, Accept function creates a new socket pRequest and returns to the handle of sentence. AsyncSelect function monitors two events of FD_READ and FD_ CLOSE in port 80. When an incoming event is FD_READ, the receiver is prepared and OnReceive ( ) function is triggered. When an incoming event is FD_WRIT, OnSend( )function will be triggered and the data is sent. Void Crequest Socket::On Receive (int nError Code) function of ReqSock.cpp will be executed when data arrive. The code int nBytes = Receive (m_buf, GetData (), m_buf, GetSize()) store the data packets uploaded from tcp layer in the m_buf buffers of request and response. Next, use the switch statement to process three kinds of request status (REQ_REQUEST, REQ_HEADER, and REQ_BODY). When requested state of m_reqStatus == Figure 1. The main workflow of HTTPSVR REQ_DONE, call Star-tResponse ( ) to began constructing the response message. 3.2. Asynchronous non-blocking CAsyncSocke Create () function of CasyncSocket In addition to creating When the response message constructing with Start Response a Socket, WSAAsyncSelect ( ) is used to associate this socket ( ) is finished, Async Select (FD_WRITE | FD_CLOSE) is with the window object so that the object can handle the events used. The void Crequest Socket :: OnSend (int nError Code) (message) from the socket. However, after CSocketWnd function is called to send the data in buffer m_buf [4]. receives the event of Socket, it just calls back the virtual functions such as Casync Socket:: On Receive () Casync Socket :: On Send (), CasyncSocket :: OnAccept (), Casync Socket :: On Connect (), etc. So CAsyncSocket derived class only needs to add code or send code in these virtual functions. When using CasyncSocket, all networks IO are asynchronous operation if you use Create to create socket by default. The network data transfer needs the following functions: OnAccept, OnClose, OnConnect, OnOut-OfBandData, OnReceive, OnSen [5]. 3.3. HTTPSVR KEY CATEGORY ANALYSIS Void CListenSocket:: OnAccept (int nErrorCode) Create a new CRequestSocket and monitor port 80. Return to a socket when requests arrive. Through AsyncSelect (FD_READ | FD_CLOSE), the void CRequestSocket :: OnReceive (int nErrorCode) function is called to receive data. 2 MATEC Web of Conferences 63, 05023 (2016) DOI: 10.1051/matecconf/20166305023 MMME 2016 Void CrequestSocket :: OnReceive (int nError-Code) All the browser by BOOL CRequestSocket :: StartResponse major features of httpsvr are achieved including accepting (void). request from web browser, analyzing the request packet according to http protocol, constructing the response message, and sending the processed data packets back to the browser. +773659352*5$0((;&87,21352&(66 Receive (m_buf, GetData ( ), m_buf, GetSize ( )) function receives the request packet from browser. Switch statement 4.1. Run Function processes it depending on the state. When the browse's request Program is started to be executed by the _tWinMain function status m_reqStatus is REQ_REQUEST, process with a while of APPMODULE.CPP file in MFC. The function of return loop. Process Line ( ) function parses the each line of received AfxWinMain is executed. packets and then initialize m_pRequest class. After the swich statement is finished, StartResponse ( ) is called according to 4.2. Link Server the parsing results of m_pRequest to construct the response A worker thread pThread is created by the int AFXAPI packet. Finally, the function AsyncSelect (FD_WRITE | AfxWinMain function in WINMAIN.CPP file and initialize htt FD_CLOSE) recalls the function OnSend to sent. psvr. The BOOL CHttpSvrApp :: InitInstance method in void CRequestSocket :: OnSend ( int nErrorCode) call int HttpSvr.cpp file is called to initialize the HTTP server and then nBytes = Send (m_buf. GetData ( ), m_cbOut ) function to run the main function of thread. Finally, int CWinThread :: send the data in the cache and process the possible errors by Run function is run in THRD-CORE.CPP file. The loop of closing or re-transmitting. server begins to run. void CRequestSocket :: ProcessLine (void) conducts Within loops, the void CLis-tenSocket OnAccept method in processing according to different states of m_reqStatus to Listen.cpp is called to generate CRequestSocket, which is set complete the initialization of CRequest * m_pRequest. for monitoring port 8080. The port is set as 8080, the address of web service file is directed to the address of root. Now, the BOOL CRequestSocket :: StartResponse (void) [6] This result is displayed as shown in Figure 2. function is used to construct the response message. It saves the constructed response packet in the cach of m_buf and sends m_buf using void CRequestSocket :: OnSend (int nErrorCode). It can construct different response packet according to different Method (GET, HEAD, POST). For example, when the Method is GET, it indicates that the browser needs to browse the web page. The FindTarget (strFile) is started to search in the default directory. If it exists, the packet is open and the http protocol requiring head is constructed using StuffHeading ( ). Then Figure 2. Link the server. using StartTargetStuff, the contents of opened web page is put into m_buf and waiting to be sent.