LwIP Application Development Notes Seven: LwIP no operating system HTTP server

  In front of us to achieve a simple TCP server and client application, then we implement an application protocol based on TCP protocol that HTTP Hypertext Transfer Protocol

1 ,   HTTP protocol Introduction  

  Hypertext Transfer Protocol (Hyper Text Transfer Protocol), HTTP for short, is an application layer protocol TCP-based, is so far one of the most popular application layer protocol, HTTP protocol can be said is the cornerstone of the World Wide Web.

  HTTP is a client requests, the server acknowledge the application transport protocol, that the server is not possible sends data to the client. Requests and responses under normal circumstances the network is one to one. And this request and the response is the back-end developers often see Request and Response.

  First, we look at the client-side requests, HTTP request packet from the request line, request headers, a blank line and request body composition. Its message format is as follows:

 

  Let's talk about the request line, which consists of a field request method, URL field and the HTTP protocol version field three fields, which are separated by spaces. It is to be understood that the method of the request, the HTTP protocol has a request method GET, POST, HEAD, PUT, DELETE, OPTIONS, TRACE, CONNECT several. First of several commonly used as follows:

  • GET method , which means to obtain the URL specified resource, this request is the easiest way and most commonly used. When using the GET method, the request parameters and the corresponding value is appended URI, using a question mark ( "?") To separate the URI of the resource request parameters and, between the parameter and the symbol ( "&") apart, Thus the length of the transmission parameters is also limited, and privacy-related information is also directly exposed to the URI. For example /index.jsp?username=holmofy&password=123123
  • HEAD method, the GET same usage, but no response to the body, there is no occasion to use a multi-GET. For example before downloading the HEAD request sent by ContentLength response field to understand the size of the network resources; or is determined by the local cache resources LastModified whether to update the response field.
  • POST method, generally used to submit information or data, the server processes the request (e.g., to submit a form or upload files). GET POST form uses relatively speaking quite secretive, and URL GET have length limitations, and upload large files it is necessary to use the POST.
  • OPTIONS method, the method for requesting the server tells what other functions and methods it supports. By OPTIONS method, you can ask what methods specific server support, or what kind of methods the server uses to deal with some special resources. Can say that this is the best way you know you can handle the resources of a method of detecting, by which the client without access to actual resources on the server. This option appears more in the case of cross-domain HTTP request, here is an article about cross-domain requests, of which a map is a good explanation of what cross-domain HTTP request. 

  The client HTTP request, the server receives, sends a response message to the client. So then, we look at the response packet on the server side. HTTP response message from the response line, in response to the first, a blank line and in response to body composition. Its message format is as follows:

 

  In response to the message, the response line is very important, which is the most important response line is the HTTP status code. HTTP protocol status code has three digits, the first digit defines the categories of responses, the following five:

  • 1XX : Information only. It represents the server request has been accepted, but the need to continue treatment, in the range of 100 to 101.
  • 2XX : Request successful. Server successfully processed the request. The range of 200 to 206.
  • 3XX:客户端重定向。重定向状态码用于告诉客户端浏览器,它们访问的资源已被移动,并告诉客户端新的资源位置。客户端收到重定向会重新对新资源发起请求。范围为300~305。
  • 4XX客户端信息错误。客户端可能发送了服务器无法处理的东西,比如请求的格式错误,或者请求了一个不存在的资源。范围为400~415。
  • 5XX:服务器出错。客户端发送了有效的请求,但是服务器自身出现错误,比如Web程序运行出错。范围是500~505。

  我们开发过程有一些状态码比较常见,我们对其简单说明如下:

 

2  HTTP服务器端的设计  

  我们已经对基于RAW API的TCP应用有了了解。我们在实现TCP服务器的实验时就提到过对于更复杂的应用和应用层协议只是在功能上的差别,从实现的结构及流程来说是完全一致的。所以对于实现HTTP服务器需要使用到的函数及整个操作流程我们就不再叙述了。重点说一说不同的地方。

  首先HTTP服务器是基于TCP的,所以其我们先将其当作TCP服务器来实现。需要注意的是,HTTP协议有其专门的操作端口:80。所以我们设计服务器时需要使用这个端口。

  在这里,我们设计一个简单的HTTP服务器,当客户端连接到服务器之后,如果收到的是html请求,则返回一个我们预先设定好的网页。正常返回这个网页,HTTP的功能就完成了,HTTP服务器会主动断开与客户端的连接。

3  TTP服务器实现  

  既然是基于TCP的HTTP服务器,我们佷显然依然按照TCP服务器的结构来实现。我们依然将其划分为三个部分来实现。首先要实现的是HTTP服务器的初始化。

 1 /* HTTP服务器初始化配置*/
 2  void Http_Server_Initialization(void)
 3 {
 4   struct tcp_pcb *pcb = NULL;                           
 5  
 6   /* 生成一个新的TCP控制块 */
 7   pcb = tcp_new();                                   
 8  
 9   /* 控制块绑定到本地IP和对应端口 */
10   tcp_bind(pcb, IP_ADDR_ANY, TCP_HTTP_SERVER_PORT);      
11  
12   /* 服务器进入侦听状态 */
13   pcb = tcp_listen(pcb);                       
14  
15   /* 注册服务器accept回调函数 */
16   tcp_accept(pcb, HttpServerAccept);  
17                                                                      
18 }

  从上面的代码不难看出,与TCP服务器的初始化一样:建立控制块,为控制块绑定本地IP和端口,服务器监听控制块同时注册接收处理回调函数。所以接下来就是实现接收处理回调函数。

1 /* HTTP接收回调函数,客户端建立连接后,本函数被调用 */
2 static err_t HttpServerAccept(void *arg, struct tcp_pcb *pcb, err_t err)
3 {
4   /*注册HTTP服务器回调函数*/
5   tcp_recv(pcb, HttpServerCallback);
6  
7   return ERR_OK;
8 }

  客户端连接成功后就会调用接收处理回调函数。该函数为tcp_accept_fn类型,注册到了监听控制块的accept字段。在这个函数中,我们需要注册HTTP服务器处理函数。其功能就由这个函数决定。

 1 /* HTTP服务器信息处理回调函数 */
 2 static err_t HttpServerCallback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
 3 {
 4   char *data = NULL;
 5  
 6   if (p != NULL)
 7   {       
 8     /* 更新接收窗口 */
 9     tcp_recved(pcb, p->tot_len);
10     data =  p->payload;
11    
12     /* 如果是http请求,返回html信息,否则无响应 */
13     if(p->len >=3 && data[0] == 'G'&& data[1] == 'E'&& data[2] == 'T')
14     {
15       tcp_write(pcb, htmlMessage, sizeof(htmlMessage), 1);
16     }
17     else
18     {
19  
20     }
21     pbuf_free(p);
22     tcp_close(pcb);
23   }
24   else if (err == ERR_OK)
25   {
26     return tcp_close(pcb);
27   }
28   return ERR_OK;
29 }

  这个HTTP服务器非常简单,我们只是实现了GET方法。也就是说,收到客户端的html请求后,我们检测其要求,如果是GET方法,我们就返回预先设定好的网页,否则无返回。然后关闭这一连接。如果我们想要实现更复杂的功能,或者需要支持HTTP协议的其他方法,只需要扩展这个函数就可以了。

4  结论  

       HTTP协议是一种使用非常广泛的协议,其基于TCP基础上运行,所以在我们前面已经实现TCP服务器及客户端的情况下,开发HTTP服务器应用就显得简单了。在这一篇我们基于LwIP实现了一个简单的HTTP服务器应用,我们并对其进行了简单的测试,虽然我们只是实现了GET方法,但经测试设计是正确的。如果需要设计其他方法的HTTP应用只需在此基础上添加即可。

欢迎关注:

Guess you like

Origin www.cnblogs.com/foxclever/p/12045235.html