实现http接口

1、安装libevent

$ tar xvzf libevent-2.0.20-stable.tar.gz
$ cd libevent-2.0.20-stable/
$ ./configure --prefix=/usr/local/libevent-2.0.20-stable/
$ make
$ make install

2、修改LD_LIBRARY_PATH

$ export LD_LIBRARY_PATH=/usr/local/libevent-2.0.20-stable/lib:$LD_LIBRARY_PATH
$ sudo ldconfig

3、简易实现

#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <event.h>
#include <evhttp.h>

/*  HTTP Request Handle  */
void http_handle(struct evhttp_request *req, void *arg); 

int main() {
    struct evhttp *httpd;
    event_init();
    httpd = evhttp_start("0.0.0.0", 2345);
    if (httpd == NULL) {
        fprintf(stderr, "Error: Unable to listen on %s:%d\n\n");
        exit(1);    
    }   
    evhttp_set_timeout(httpd, 2000);
    evhttp_set_gencb(httpd, http_handle, NULL);
    event_dispatch();
    evhttp_free(httpd);

    return 0;
}

void http_handle(struct evhttp_request *req, void *arg){
    struct evbuffer *buf;
    buf = evbuffer_new();

    /*  Response the client  */
    evhttp_send_reply(req, HTTP_OK, "OK", buf);

    /*  Release the memory  */
    evbuffer_free(buf);
    fprintf(stderr, "收到请求\n");
}

4、编译

$ gcc http.c -L /usr/local/libevent-2.0.20-stable/lib/ -levent -I /usr/local/libevent-2.0.20-stable/include/

5、测试

(1)服务端执行编译后的文件

$ ./a.out

(2)客户端发送请求

$ curl http://127.0.0.1:2345


 

6、处理http请求,重写http_handle方法

void http_handle(struct evhttp_request *req, void *arg){
    struct evbuffer *buf;
    buf = evbuffer_new();

    /*  Analyst the URI  */
    char *decode_uri = strdup((char*) evhttp_request_uri(req));
    struct evkeyvalq http_query;
    evhttp_parse_query(decode_uri, &http_query);
    free(decode_uri);

    /*  URI Parameter  */
    const char *http_input_opt = evhttp_find_header (&http_query, "opt"); 
    const char *http_input_name = evhttp_find_header (&http_query, "name");
    const char *http_input_data = evhttp_find_header (&http_query, "data"); 

    /*  header  */
    evhttp_add_header(req->output_headers, "Content-Type", "text/plain");
    evhttp_add_header(req->output_headers, "Connection", "keep-alive");
    evhttp_add_header(req->output_headers, "Cache-Control", "no-cache");

    if(http_input_opt != NULL && http_input_name != NULL && strlen(http_input_name) < 100){
        if(strcmp(http_input_opt, "GET") == 0){
            int buffer_data_len = EVBUFFER_LENGTH(req->input_buffer);
            if(buffer_data_len > 0){ 
                /* POST METHOD */
                char *input_value_data = EVBUFFER_DATA(req->input_buffer);
                fprintf(stderr,"%s \n",input_value_data);
            }else if(http_input_data != NULL){
                fprintf(stderr,"opt:%s \n",http_input_opt);
                fprintf(stderr,"name:%s \n",http_input_name);
                fprintf(stderr,"data:%s \n",http_input_data);
            }
        }
    }

    /* Response */
    evhttp_send_reply(req, HTTP_OK, "OK", buf);

    /* Release the memory */
    evhttp_clear_headers(&http_query);
    evbuffer_free(buf);
}

  

7、客户端发送请求

$ curl http://127.0.0.1:2345/?opt=GET&name=getUser&data=1

 

猜你喜欢

转载自eric-gao.iteye.com/blog/2205044