第二章 C语言实例 —制作http服务器
任务:
?? 1.制作http服务器,读取url提交的相关数据.
?? 2.把读到的数据推入到队列中.
?
?
条件:
使用libevent的类库,所以先安装libevent
?
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"); /* Operation Type */ const char *http_input_name = evhttp_find_header (&http_query, "name"); /* Queue Name */ const char *http_input_data = evhttp_find_header (&http_query, "data"); /* Data With GET */ /* 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"); evhttp_add_header(req->output_headers, "author", "Dennis .Z Ritchie"); if(http_input_opt != NULL && http_input_name != NULL && strlen(http_input_name) < 300){ /* GET Method,OUT The Queue */ if(strcmp(http_input_opt,"put") == 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); /* Submited Data */ fprintf(stderr,"%s \n",input_value_data); }else if(http_input_data != NULL){ fprintf(stderr,"%s \n",http_input_data); } }else if(strcmp(http_input_opt,"get") == 0){ } } /* Response the client */ evhttp_send_reply(req, HTTP_OK, "OK", buf); //evbuffer_add_printf(buf, "%s", "HTTPSQS_AUTH_FAILED"); /* Release the memory */ evhttp_clear_headers(&http_query); evbuffer_free(buf);}??
?
?
?
?