第二章 C语言实例 —制作http服务器 dennis ITeye技术网站

第二章 C语言实例 —制作http服务器 - dennis - ITeye技术网站

任务:

   1.制作http服务器,读取url提交的相关数据.

   2.把读到的数据推入到队列中.

条件:

使用libevent的类库,所以先安装libevent

Sh代码  收藏代码
  1. tar zxvf libevent-2.0.12-stable.tar.gz  
  2. cd libevent-2.0.12-stable/  
  3. ./configure --prefix=/usr/local/libevent-2.0.12-stable/  
  4. make  
  5. make install  
  6. cd ../  

1.服务端简易代码如下

C代码  收藏代码
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. #include <err.h>  
  5. #include <event.h>  
  6. #include <evhttp.h>  
  7.   
  8. void http_handle(struct evhttp_request *req, void *arg); /*  HTTP Request Handle  */  
  9.   
  10. int main(){  
  11.     struct evhttp *httpd;  
  12.     event_init();  
  13.     httpd = evhttp_start("0.0.0.0", 2345);  
  14.     if (httpd == NULL) {  
  15.         fprintf(stderr, "Error: Unable to listen on %s:%d\n\n");  
  16.         exit(1);      
  17.     }     
  18.     evhttp_set_timeout(httpd, 2000);  
  19.     evhttp_set_gencb(httpd, http_handle, NULL);  
  20.     event_dispatch();  
  21.     evhttp_free(httpd);  
  22.   
  23.     return 0;  
  24. }  
  25.   
  26. void http_handle(struct evhttp_request *req, void *arg){  
  27.     struct evbuffer *buf;  
  28.     buf = evbuffer_new();  
  29.   
  30.     /*  Response the client  */  
  31.     evhttp_send_reply(req, HTTP_OK, "OK", buf);  
  32.   
  33.     //evbuffer_add_printf(buf, "%s", "HTTPSQS_AUTH_FAILED");  
  34.   
  35.     /*  Release the memory  */  
  36.     evbuffer_free(buf);  
  37.     fprintf(stderr,"Send \n");  
  38. }  

编译:(编译时把libevent的类库中的.so文件和.h文件连接
进来)

Sh代码  收藏代码
  1. gcc http.c -L/usr/local/libevent-2.0.12-stable/lib/ -levent -I/usr/local/libevent-2.0.12-stable/include/  

测试

在服务器端,执行编译后的文件a.out

Java代码  收藏代码
  1. ./a.out  

客户端进行访问(也可以使用浏览器访问,但是要打开端口号2345——vi /etc/sysconfig/iptables)

Java代码  收藏代码
  1. [www@zhoubc data]$Snbsp;curl http://127.0.0.1:2345  

此时再看服务端,变成如下状态

Java代码  收藏代码
  1. [www@zhoubc queue]$Snbsp;./a.out   
  2. Send   

2.处理http请求,重写htt_handle方法

C代码  收藏代码
  1. void http_handle(struct evhttp_request *req, void *arg){  
  2.     struct evbuffer *buf;  
  3.     buf = evbuffer_new();  
  4.   
  5.     /*  Analyst the URI  */  
  6.     char *decode_uri = strdup((char*) evhttp_request_uri(req));  
  7.     struct evkeyvalq http_query;  
  8.     evhttp_parse_query(decode_uri, &http_query);  
  9.     free(decode_uri);  
  10.   
  11.     /*  URI Parameter  */  
  12.     const char *http_input_opt = evhttp_find_header (&http_query, "opt"); /* Operation Type */  
  13.     const char *http_input_name = evhttp_find_header (&http_query, "name"); /* Queue Name */  
  14.     const char *http_input_data = evhttp_find_header (&http_query, "data"); /* Data With GET */  
  15.   
  16.     /*  header  */  
  17.     evhttp_add_header(req->output_headers, "Content-Type""text/plain");  
  18.     evhttp_add_header(req->output_headers, "Connection""keep-alive");  
  19.     evhttp_add_header(req->output_headers, "Cache-Control""no-cache");  
  20.     evhttp_add_header(req->output_headers, "author""Dennis .Z Ritchie");  
  21.   
  22.     if(http_input_opt != NULL && http_input_name != NULL && strlen(http_input_name) < 300){  
  23.         /*  GET Method,OUT The Queue  */  
  24.         if(strcmp(http_input_opt,"put") == 0){  
  25.             int buffer_data_len = EVBUFFER_LENGTH(req->input_buffer);  
  26.             if(buffer_data_len > 0){ /* POST METHOD */  
  27.                 char *input_value_data = EVBUFFER_DATA(req->input_buffer); /* Submited Data */  
  28.                 fprintf(stderr,"%s \n",input_value_data);  
  29.             }else if(http_input_data != NULL){  
  30.                 fprintf(stderr,"%s \n",http_input_data);  
  31.             }  
  32.         }else if(strcmp(http_input_opt,"get") == 0){  
  33.         }  
  34.     }  
  35.   
  36.     /*  Response the client  */  
  37.     evhttp_send_reply(req, HTTP_OK, "OK", buf);  
  38.   
  39.     //evbuffer_add_printf(buf, "%s", "HTTPSQS_AUTH_FAILED");  
  40.   
  41.     /*  Release the memory  */  
  42.     evhttp_clear_headers(&http_query);  
  43.   
  44.     evbuffer_free(buf);  
  45. }  
原文地址:https://www.cnblogs.com/lexus/p/2596811.html