void web_pipeline_get(struct evhttp_request *req, void *arg) { moModule *module; struct evkeyvalq headers; const char *uri; uri = evhttp_request_uri(req); if ( uri == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "unable to retreive uri"); } evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "objectname") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing objectname"); } if ( evhttp_find_header(&headers, "name") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing name"); } module = pipeline->getModuleById(evhttp_find_header(&headers, "objectname")); if ( module == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "object not found"); } web_message(req, module->property(evhttp_find_header(&headers, "name")).asString().c_str()); evhttp_clear_headers(&headers); }
void web_pipeline_create(struct evhttp_request *req, void *arg) { moModule *module; struct evkeyvalq headers; const char *uri; uri = evhttp_request_uri(req); if ( uri == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "unable to retreive uri"); } evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "objectname") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing objectname"); } module = moFactory::getInstance()->create(evhttp_find_header(&headers, "objectname")); if ( module == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "invalid objectname"); } pipeline->addElement(module); evhttp_clear_headers(&headers); web_message(req, module->property("id").asString().c_str()); }
void request_handler(struct evhttp_request *req, void *arg) { const char *uri; uri = evhttp_request_uri(req); if(strncmp(uri, "/updates/", 9) != 0){ evhttp_send_error(req, HTTP_NOTFOUND, "Not Found"); if (settings.verbose > 0) fprintf(stderr, "URL not found.. needs to be /updates/... but was %s\n", uri); return; } const char *rkey; struct evkeyvalq args; TAILQ_INIT(&args); evhttp_parse_query(uri, &args); rkey = evhttp_find_header(&args, "rkey"); if (NULL == rkey) { evhttp_send_error(req, HTTP_BADREQUEST, "Bad Request"); if (settings.verbose > 0) fprintf(stderr, "RKey param not found in request URI %s\n", uri); evhttp_clear_headers(&args); return; } fprintf(stderr, "Using RKey: %s\n", rkey); char *cached = fetch_memcached(rkey); if (NULL == cached) { evhttp_send_error(req, HTTP_BADREQUEST, "Bad Request"); fprintf(stderr, "RKey %s not found in Memcache!\n", rkey); evhttp_clear_headers(&args); return; } int uid = atoi(cached); fprintf(stderr, "Great, found RKey in Memcached: %s = %s and now UID %d\n", rkey, cached, uid); struct evbuffer *buf = evbuffer_new(); evhttp_add_header(req->output_headers, "Content-Type", "text/html; charset=UTF-8"); evhttp_add_header(req->output_headers, "Connection", "keep-alive"); evhttp_add_header(req->output_headers, "Cache-Control", "no-cache"); evhttp_send_reply_start(req, HTTP_OK, "OK"); evbuffer_add_printf(buf, "Welcome, RKey: ‘%s’\n", rkey); evhttp_send_reply_chunk(req, buf); evbuffer_free(buf); clients[uid] = req; evhttp_clear_headers(&args); free(cached); evhttp_connection_set_closecb( req->evcon, cleanup, &slots[uid] ); }
void req_handler (struct evhttp_request *req, void *arg) { struct evbuffer *buf = evbuffer_new(); D_("event handler invoked\n"); struct evbuffer *body = evhttp_request_get_input_buffer(req); size_t len = evbuffer_get_length(body); D_("size:%d, %d", len, req->body_size); if(req->type == EVHTTP_REQ_GET) { //evbuffer_add_printf(buf, "Reqested GET: %s\n", evhttp_request_uri(r)); //evhttp_send_reply(r, HTTP_OK, "OK", buf); dump_http_header(req, buf, NULL); } else if(req->type == EVHTTP_REQ_POST) { // now, we fetch key values unsigned char *requestLine; requestLine = evbuffer_pullup(body, -1); // need to terminate; requestLine[len] = '\0'; //D_("len:%lu, line:'%s'",readsize, requestLine); //now, parse json. struct dse_request_t *dreq = dse_parseJson((const char*)requestLine); // dse_dispatch(dreq->method); evbuffer_add_printf(buf, "Reqested POSPOS: %s\n", evhttp_request_uri(req)); evhttp_send_reply(req, HTTP_OK, "OK", buf); } else { evhttp_send_error(req, HTTP_BADREQUEST, "Available GET only"); } }
void request_handler(struct evhttp_request *req, void *arg) { // resolve route char *decode_uri = strdup((char *)evhttp_request_uri(req)); fprintf(stdout , "request: %s\n", decode_uri); int i = 0; for (; ;) { if (route_table[i].prefix == NULL && route_table[i].type == 0) { break; } enum evhttp_cmd_type type = evhttp_request_get_command(req); if (strncmp(route_table[i].prefix, decode_uri, strlen(route_table[i].prefix)) == 0 && route_table[i].type == type) { route_table[i].callback(req, arg); goto end; } i++; } send_404_page(req); fprintf(stderr, "404:%s\n", decode_uri); end: free(decode_uri); return; }
void web_pipeline_set(struct evhttp_request *req, void *arg) { moModule *module; struct evkeyvalq headers; const char *uri; uri = evhttp_request_uri(req); evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "objectname") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing objectname"); } if ( evhttp_find_header(&headers, "name") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing name"); } if ( evhttp_find_header(&headers, "value") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing value"); } module = module_search(evhttp_find_header(&headers, "objectname"), pipeline); if ( module == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "object not found"); } module->property(evhttp_find_header(&headers, "name")).set( (const std::string &)evhttp_find_header(&headers, "value")); evhttp_clear_headers(&headers); web_message(req, "ok"); }
void web_pipeline_remove(struct evhttp_request *req, void *arg) { moModule *module; moDataStream *ds; struct evkeyvalq headers; const char *uri; uri = evhttp_request_uri(req); if ( uri == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "unable to retreive uri"); } evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "objectname") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing objectname"); } module = pipeline->getModuleById(evhttp_find_header(&headers, "objectname")); if ( module == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "object not found"); } pipeline->stop(); module->stop(); // disconnect inputs if ( module->getInputCount() ) { for ( int i = 0; i < module->getInputCount(); i++ ) { ds = module->getInput(i); if ( ds == NULL ) continue; ds->removeObserver(module); } } // disconnect output if ( module->getOutputCount() ) { for ( int i = 0; i < module->getOutputCount(); i++ ) { ds = module->getOutput(i); if ( ds == NULL ) continue; ds->removeObservers(); } } // remove element from pipeline pipeline->removeElement(module); delete module; web_message(req, "ok"); evhttp_clear_headers(&headers); }
void CHttpServer::http_handle_postdata(struct evhttp_request *req, void *arg) { CHttpServer *pthis = (CHttpServer *)arg; // 请求是用POST发送的,下面的evhttp_request_uri和evhttp_parse_query函数 // 不能正确解析uri中的参数,Libevent库的BUG! char *decode_uri; struct evkeyvalq params; decode_uri = strdup((char *)evhttp_request_uri(req)); evhttp_parse_query(decode_uri, ¶ms); free(decode_uri); // POST int buffer_data_len; buffer_data_len = EVBUFFER_LENGTH(req->input_buffer); //char *post_data; if (buffer_data_len) { char *buffer_data = (char *) malloc(buffer_data_len + 1); memset(buffer_data, '\0', buffer_data_len + 1); memcpy(buffer_data, EVBUFFER_DATA(req->input_buffer), buffer_data_len); //post_data = (char *) EVBUFFER_DATA(req->input_buffer); //printf("------------------------start---------------------------------------------\n"); //printf("%s\n", buffer_data); //printf("------------------------end-----------------------------------------------\n"); //if(NULL == buffer_data) //return; http_reponse(req, ¶ms, 200, "OK"); // 转发给云评估系统 size_t stat_code; char *uri = g_confvalue.url_path; stat_code = httpPostAsyn(uri, buffer_data, strlen(buffer_data)+1, NULL); //logrun("http trans response code:[%d]", stat_code); // 缓存数据等待重发 if (stat_code != 200) { //200:OK #ifdef DEBUG printf("forward datas fail, datas to buffer.\n"); //pthread_mutex_lock(&pthis->m_mutex); logbuf("%s", buffer_data); //pthread_mutex_unlock(&pthis->m_mutex); #else logrun("forward datas fail, datas to buffer."); //pthread_mutex_lock(&pthis->m_mutex); logbuf("%s", buffer_data); //pthread_mutex_unlock(&pthis->m_mutex); #endif } // 解析收到的json格式的OpenStack数据 parse_openstack_data(arg, req, params, buffer_data); free(buffer_data); } else { logrun("rece post data nothing."); http_reponse(req, ¶ms, 400, "ERROR"); } return; }
void generic_handler(struct evhttp_request *req, void *arg) { struct evbuffer *buf; buf = evbuffer_new(); if (buf == NULL) err(1, "failed to create response buffer"); evbuffer_add_printf(buf, "Requested: %s/n", evhttp_request_uri(req)); evhttp_send_reply(req, HTTP_OK, "OK", buf); }
void web_pipeline_stream(struct evhttp_request *req, void *arg) { struct timeval when = { 0, 20 }; struct evkeyvalq headers; const char *uri; int idx = 0; moModule *module = NULL; uri = evhttp_request_uri(req); evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "objectname") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing objectname"); } module = module_search(evhttp_find_header(&headers, "objectname"), pipeline); if ( module == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "object not found"); } if ( evhttp_find_header(&headers, "index") != NULL ) idx = atoi(evhttp_find_header(&headers, "index")); if ( idx < 0 || idx >= module->getOutputCount() ) { evhttp_clear_headers(&headers); return web_error(req, "invalid index"); } struct chunk_req_state *state = (struct chunk_req_state*)malloc(sizeof(struct chunk_req_state)); memset(state, 0, sizeof(struct chunk_req_state)); state->req = req; state->closed = false; state->stream = new otStreamModule(); state->delay = 100; if ( evhttp_find_header(&headers, "scale") != NULL ) state->stream->property("scale").set(evhttp_find_header(&headers, "scale")); if ( evhttp_find_header(&headers, "delay") != NULL ) state->delay = atoi(evhttp_find_header(&headers, "delay")); state->stream->setInput(module->getOutput(idx)); evhttp_add_header(req->output_headers, "Content-Type", "multipart/x-mixed-replace; boundary=mjpegstream"); evhttp_send_reply_start(req, HTTP_OK, "Everything is fine"); evhttp_connection_set_closecb(req->evcon, web_pipeline_stream_close, state); event_once(-1, EV_TIMEOUT, web_pipeline_stream_trickle, state, &when); }
static VALUE aspirin_response_create_env(VALUE obj, VALUE default_env) { VALUE env = rb_obj_dup(default_env); Aspirin_Response *response; Data_Get_Struct(obj, Aspirin_Response, response); struct evhttp_request *request = response->request; set_rack_input(env, request->input_buffer); set_rack_errors(env); set_request_method(env, request->type); set_remote_host(env, request->remote_host); set_http_version(env, request->major, request->minor); set_async_callback(env, obj); set_request_uri(env, evhttp_request_uri(request)); set_request_path(env, evhttp_request_uri(request)); set_http_header(env, request->input_headers); return env; }
/* 处理模块 */ void httpcws_handler(struct evhttp_request *req, void *arg) { struct evbuffer *buf; buf = evbuffer_new(); /* 分析URL参数 */ struct evkeyvalq httpcws_http_query; evhttp_parse_query(evhttp_request_uri(req), &httpcws_http_query); /* 接收POST表单信息 */ const char *tcsql_input_postbuffer = (const char*) EVBUFFER_DATA(req->input_buffer); /* 接收GET表单参数 */ const char *httpcws_input_words = evhttp_find_header (&httpcws_http_query, "w"); const char *httpcws_output_tmp = NULL; char *httpcws_output_words = "\0"; if (tcsql_input_postbuffer != NULL) { char *tcsql_input_postbuffer_tmp = (char *) malloc(EVBUFFER_LENGTH(req->input_buffer)+1); memset (tcsql_input_postbuffer_tmp, '\0', EVBUFFER_LENGTH(req->input_buffer)+1); strncpy(tcsql_input_postbuffer_tmp, tcsql_input_postbuffer, EVBUFFER_LENGTH(req->input_buffer)); char *decode_uri = urldecode(tcsql_input_postbuffer_tmp); free(tcsql_input_postbuffer_tmp); httpcws_output_tmp = ICTCLAS_ParagraphProcess(decode_uri, 0); free(decode_uri); httpcws_output_words = strdup(httpcws_output_tmp); trim (httpcws_output_words); } else if (httpcws_input_words != NULL) { char *httpcws_input_words_tmp = strdup(httpcws_input_words); char *decode_uri = urldecode(httpcws_input_words_tmp); free(httpcws_input_words_tmp); httpcws_output_tmp = ICTCLAS_ParagraphProcess(decode_uri, 0); free(decode_uri); httpcws_output_words = strdup(httpcws_output_tmp); trim (httpcws_output_words); } else { httpcws_output_words = strdup(""); } /* 输出内容给客户端 */ evhttp_add_header(req->output_headers, "Server", "HTTPCWS/1.0.0"); evhttp_add_header(req->output_headers, "Content-Type", "text/plain; charset=GB2312"); evhttp_add_header(req->output_headers, "Connection", "close"); evbuffer_add_printf(buf, "%s", httpcws_output_words); evhttp_send_reply(req, HTTP_OK, "OK", buf); free(httpcws_output_words); evhttp_clear_headers(&httpcws_http_query); evbuffer_free(buf); }
void web_pipeline_connect(struct evhttp_request *req, void *arg) { moModule *in, *out; int inidx = 0, outidx = 0; struct evkeyvalq headers; const char *uri; uri = evhttp_request_uri(req); if ( uri == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "unable to retreive uri"); } evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "out") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing out"); } if ( evhttp_find_header(&headers, "in") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing in"); } if ( evhttp_find_header(&headers, "outidx") != NULL ) outidx = atoi(evhttp_find_header(&headers, "outidx")); if ( evhttp_find_header(&headers, "inidx") != NULL ) inidx = atoi(evhttp_find_header(&headers, "inidx")); in = pipeline->getModuleById(evhttp_find_header(&headers, "in")); out = pipeline->getModuleById(evhttp_find_header(&headers, "out")); if ( in == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "in object not found"); } if ( out == NULL && strcmp(evhttp_find_header(&headers, "out"), "NULL") != 0 ) { evhttp_clear_headers(&headers); return web_error(req, "out object not found"); } if ( strcmp(evhttp_find_header(&headers, "out"), "NULL") == 0 ) in->setInput(NULL, inidx); else in->setInput(out->getOutput(outidx), inidx); evhttp_clear_headers(&headers); web_message(req, "ok"); }
static void httpd_handler(struct evhttp_request *req, void *arg) { struct evbuffer *buf; buf = evbuffer_new(); //get http request url char *decode_uri = strdup((char*) evhttp_request_uri(req)); struct evkeyvalq httpd_query; evhttp_parse_query(decode_uri, &httpd_query); free(decode_uri); //recive http params const char *start = evhttp_find_header(&httpd_query, "start"); const char *httpd_charset = evhttp_find_header(&httpd_query, "charset"); //send http header charset if (httpd_charset != NULL && strlen(httpd_charset) <= 40) { char content_type[64] = { 0 }; sprintf(content_type, "text/plain; charset=%s", httpd_charset); evhttp_add_header(req->output_headers, "Content-Type", content_type); } else { 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"); evbuffer_add_printf(buf, "%s\n", "HTTRACK_OK"); char start_t[255] = {0x00}; sprintf(start_t, "%s", start); if (strcmp(start_t, "1") == 0) { struct hts_proj proj; proj.depth = 5; proj.priority = PRIORITY_ONLY_HTML; proj.action = ACTION_ONLY_FILES; sprintf(proj.name, "%s", "dianying.yisou.com"); sprintf(proj.urls, "%s", "http://dianying.yisou.com/"); new_dl_thread(); } //send content to client evhttp_send_reply(req, HTTP_OK, "OK", buf); //free buf evhttp_clear_headers(&httpd_query); evbuffer_free(buf); }
T Request_new(struct evhttp_request *req, void *data) { T R; struct evkeyval *val; R = g_malloc0(sizeof(*R)); R->req = req; R->data = data; R->uri = evhttp_decode_uri(evhttp_request_uri(R->req)); R->parts = g_strsplit_set(R->uri,"/?",0); Request_parse_getvars(R); Request_parse_postvars(R); TRACE(TRACE_DEBUG,"R->uri: [%s]", R->uri); TAILQ_FOREACH(val, R->req->input_headers, next) TRACE(TRACE_DEBUG,"input_header: [%s: %s]", val->key, val->value); // // uri-parts: /controller/id/method/arg // if (EXISTS(R->parts[1])) { R->controller = R->parts[1]; TRACE(TRACE_DEBUG,"R->controller: [%s]", R->controller); if (EXISTS(R->parts[2])) { R->id = R->parts[2]; TRACE(TRACE_DEBUG,"R->id: [%s]", R->id); if (EXISTS(R->parts[3])) { R->method = R->parts[3]; TRACE(TRACE_DEBUG,"R->method: [%s]", R->method); if (EXISTS(R->parts[4])) { R->arg = R->parts[4]; TRACE(TRACE_DEBUG,"R->arg: [%s]", R->arg); } } } } return R; }
/* 处理模块 */ void synchttp_handler(struct evhttp_request *req, void *arg) { struct evbuffer *buf; buf = evbuffer_new(); /* 分析URL参数 */ char *decode_uri = strdup((char*) evhttp_request_uri(req)); struct evkeyvalq synchttp_http_query; evhttp_parse_query(decode_uri, &synchttp_http_query); free(decode_uri); /* 接收GET表单参数 */ const char *synchttp_input_charset = evhttp_find_header(&synchttp_http_query, "charset"); /* 编码方式 */ const char *synchttp_input_data = evhttp_find_header(&synchttp_http_query, "data"); /* 数据 */ const char *synchttp_input_auth = evhttp_find_header(&synchttp_http_query, "auth"); /* 验证密码 */ /* 返回给用户的Header头信息 */ if (synchttp_input_charset != NULL && strlen(synchttp_input_charset) <= 40) { char content_type[64] = { 0x00 }; sprintf(content_type, "text/plain; charset=%s", synchttp_input_charset); evhttp_add_header(req->output_headers, "Content-Type", content_type); } else { 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"); /* 权限校验 */ bool is_auth_pass = false; /* 是否验证通过 */ if (synchttp_settings_auth != NULL) { /* 如果命令行启动参数设置了验证密码 */ if (synchttp_input_auth != NULL && strcmp(synchttp_settings_auth, synchttp_input_auth) == 0) { is_auth_pass = true; } else { is_auth_pass = false; } } else { /* 如果命令行启动参数没有设置验证密码 */ is_auth_pass = true; } if (is_auth_pass == false) { /* 校验失败 */ evbuffer_add_printf(buf, "%s", "SYNCHTTP_AUTH_FAILED"); goto output; } int now_putpos = synchttp_now_putpos(); if(now_putpos > SYNCHTTP_DEFAULT_MAXQUEUE){ evbuffer_add_printf(buf, "%s", "SYNCHTTP_PUT_FULL"); goto output; } int buffer_data_len; char queue_name[32] = { 0x00 }; sprintf(queue_name,"Q:%d", now_putpos); /*请求消息入库*/ char *synchttp_input_buffer; char *buffer_data; if (synchttp_input_data != NULL) { /*GET请求*/ buffer_data_len = strlen(synchttp_input_data); buffer_data = (char *) tccalloc(1, buffer_data_len + 3); memcpy(buffer_data, synchttp_input_data, buffer_data_len); strcat(buffer_data, "#1"); } else { /*POST请求*/ buffer_data_len = EVBUFFER_LENGTH(req->input_buffer); buffer_data = (char *) tccalloc(1, buffer_data_len + 3); memcpy(buffer_data, EVBUFFER_DATA(req->input_buffer), buffer_data_len); strcat(buffer_data, "#2"); } synchttp_input_buffer = urldecode(buffer_data); /*参数是否存在判断 */ if (strlen(synchttp_input_buffer) < 3){ /* 参数错误 */ evbuffer_add_printf(buf, "%s", "SYNCHTTP_ERROR"); goto output; } if(tcbdbput2(synchttp_db_tcbdb, queue_name, synchttp_input_buffer) == true){ fprintf(stderr, "in:%s--->%s\n", queue_name, synchttp_input_buffer); }else{ evbuffer_add_printf(buf, "%s", "SYNCHTTP_ERROR"); goto output; } evbuffer_add_printf(buf, "%s", "SYNCHTTP_SET_OK"); free(synchttp_input_buffer); free(buffer_data); output: /* 输出内容给客户端 */ evhttp_send_reply(req, HTTP_OK, "OK", buf); /* 内存释放 */ evhttp_clear_headers(&synchttp_http_query); evbuffer_free(buf); }
/* 处理模块 */ void httpsqs_handler(struct evhttp_request *req, void *arg) { struct evbuffer *buf; buf = evbuffer_new(); /* 分析URL参数 */ char *decode_uri = strdup((char*) evhttp_request_uri(req)); struct evkeyvalq httpsqs_http_query; evhttp_parse_query(decode_uri, &httpsqs_http_query); free(decode_uri); /* 接收GET表单参数 */ const char *httpsqs_input_name = evhttp_find_header (&httpsqs_http_query, "name"); /* 队列名称 */ const char *httpsqs_input_charset = evhttp_find_header (&httpsqs_http_query, "charset"); /* 操作类别 */ const char *httpsqs_input_opt = evhttp_find_header (&httpsqs_http_query, "opt"); /* 操作类别 */ const char *httpsqs_input_data = evhttp_find_header (&httpsqs_http_query, "data"); /* 操作类别 */ /* 返回给用户的Header头信息 */ if (httpsqs_input_charset != NULL && strlen(httpsqs_input_charset) <= 40) { char *content_type = (char *)malloc(64); memset(content_type, '\0', 64); sprintf(content_type, "text/plain; charset=%s", httpsqs_input_charset); evhttp_add_header(req->output_headers, "Content-Type", content_type); free(content_type); } else { 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 (httpsqs_input_name != NULL && httpsqs_input_opt != NULL && strlen(httpsqs_input_name) <= 256) { /* 入队列 */ if (strcmp(httpsqs_input_opt, "put") == 0) { /* 优先接收POST正文信息 */ int buffer_data_len; buffer_data_len = EVBUFFER_LENGTH(req->input_buffer); char* httpsqs_input_postbuffer = NULL; char* buffer_data = NULL; if (buffer_data_len <=0 && httpsqs_input_data == NULL) { evbuffer_add_printf(buf, "%s", "HTTPSQS_PUT_ERROR"); } else { if (buffer_data_len > 0) { buffer_data = (char *)malloc(buffer_data_len + 1); memset(buffer_data, '\0', buffer_data_len + 1); memcpy (buffer_data, EVBUFFER_DATA(req->input_buffer), buffer_data_len); httpsqs_input_postbuffer = urldecode(buffer_data); /* 如果POST正文无内容,则取URL中data参数的值 */ } else if (httpsqs_input_data != NULL) { buffer_data_len = strlen(httpsqs_input_data); buffer_data = (char *)malloc(buffer_data_len + 1); memset(buffer_data, '\0', buffer_data_len + 1); memcpy (buffer_data, httpsqs_input_data, buffer_data_len); httpsqs_input_postbuffer = urldecode(buffer_data); } redisReply* reply = (redisReply*)redisCommand(redis_client, "RPUSH %s %s", httpsqs_input_name, httpsqs_input_postbuffer); if (reply == NULL) { fprintf(stderr, "Put queue message failed:%s [error]:%d", httpsqs_input_name, reply->type); evbuffer_add_printf(buf, "%s", "HTTPSQS_PUT_ERROR"); } else { evbuffer_add_printf(buf, "%s", "HTTPSQS_PUT_OK"); freeReplyObject(reply); } if (httpsqs_input_postbuffer != NULL) free(httpsqs_input_postbuffer); if (buffer_data != NULL) free(buffer_data); } } else if (strcmp(httpsqs_input_opt, "get") == 0) { /* 出队列 */ redisReply* reply = (redisReply*)redisCommand(redis_client, "LPOP %s", httpsqs_input_name); if (reply == NULL) { evbuffer_add_printf(buf, "%s", "HTTPSQS_GET_ERROR"); } else if (reply->type == REDIS_REPLY_NIL){ evbuffer_add_printf(buf, "%s", "HTTPSQS_GET_END"); freeReplyObject(reply); } else { evbuffer_add_printf(buf, "%s", reply->str); freeReplyObject(reply); } } else if (strcmp(httpsqs_input_opt, "status") == 0) { /* 查看队列状态(普通浏览方式) */ evbuffer_add_printf(buf, "#TODO"); } else if (strcmp(httpsqs_input_opt, "status_json") == 0) { /* 查看队列状态(JSON方式,方便客服端程序处理) */ evbuffer_add_printf(buf, "#TODO"); } else if (strcmp(httpsqs_input_opt, "view") == 0 ) { /* 查看单条队列内容 */ evbuffer_add_printf(buf, "#TODO"); } else { /* 命令错误 */ evbuffer_add_printf(buf, "%s", "HTTPSQS_ERROR"); } } else { /* 命令错误 */ evbuffer_add_printf(buf, "%s", "HTTPSQS_ERROR"); } /* 输出内容给客户端 */ evhttp_send_reply(req, HTTP_OK, "OK", buf); /* 内存释放 */ evhttp_clear_headers(&httpsqs_http_query); evbuffer_free(buf); }
void generic_handler(struct evhttp_request *req, void *arg) { struct evbuffer *buf; struct evkeyvalq *input_headers; struct evkeyval *header; buf = evbuffer_new(); if (buf == NULL) err(1, "failed to create response buffer"); input_headers = req->input_headers; TAILQ_FOREACH(header, input_headers, next) { printf("%s :", header->key ); printf("%s \n", header->value ); evbuffer_add_printf(buf,system("/bin/ls > tmpout")); } evbuffer_add_printf(buf, "Requested: %s\n\n", evhttp_request_uri(req)); char command[100]="/bin/cat "; char tmpText[500]; char lastText[1000000]=""; strcat(command,evhttp_request_uri(req)); strcat(command," > tmp"); system(command); FILE *tmp = fopen("tmp","r"); int tmpState; while(1){ tmpState=fgets(tmpText,sizeof(tmpText),tmp); if(tmpState==NULL){ break; } strcat(lastText,"<br>"); strcat(lastText,tmpText);
void web_factory_desribe(struct evhttp_request *req, void *arg) { std::map<std::string, moProperty*>::iterator it; cJSON *root, *mod, *properties, *io, *array; moDataStream *ds; moModule *module; struct evkeyvalq headers; const char *uri; uri = evhttp_request_uri(req); evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "name") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing name"); } module = moFactory::getInstance()->create(evhttp_find_header(&headers, "name")); if ( module == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "invalid name"); } root = cJSON_CreateObject(); cJSON_AddNumberToObject(root, "success", 1); cJSON_AddStringToObject(root, "message", "ok"); cJSON_AddItemToObject(root, "describe", mod=cJSON_CreateObject()); cJSON_AddStringToObject(mod, "name", module->getName().c_str()); cJSON_AddStringToObject(mod, "description", module->getDescription().c_str()); cJSON_AddStringToObject(mod, "author", module->getAuthor().c_str()); cJSON_AddNumberToObject(mod, "running", module->isStarted() ? 1 : 0); cJSON_AddItemToObject(mod, "properties", properties=cJSON_CreateObject()); for ( it = module->getProperties().begin(); it != module->getProperties().end(); it++ ) { cJSON_AddStringToObject(properties, it->first.c_str(), it->second->asString().c_str()); } if ( module->getInputCount() ) { cJSON_AddItemToObject(mod, "inputs", array=cJSON_CreateArray()); for ( int i = 0; i < module->getInputCount(); i++ ) { ds = module->getInput(i); cJSON_AddItemToArray(array, io=cJSON_CreateObject()); cJSON_AddNumberToObject(io, "index", i); cJSON_AddStringToObject(io, "name", module->getInputInfos(i)->getName().c_str()); cJSON_AddStringToObject(io, "type", module->getInputInfos(i)->getType().c_str()); } } if ( module->getOutputCount() ) { cJSON_AddItemToObject(mod, "outputs", array=cJSON_CreateArray()); for ( int i = 0; i < module->getOutputCount(); i++ ) { ds = module->getOutput(i); cJSON_AddItemToArray(array, io=cJSON_CreateObject()); cJSON_AddNumberToObject(io, "index", i); cJSON_AddStringToObject(io, "name", module->getOutputInfos(i)->getName().c_str()); cJSON_AddStringToObject(io, "type", module->getOutputInfos(i)->getType().c_str()); } } delete module; evhttp_clear_headers(&headers); web_json(req, root); }
// Process a request // Call the default callback when a uri isn't found void process_request(struct evhttp_request *req, void *arg) { // Form a Request object from the evhttp request data, and call the // appropriate callback. Then, we return a string in the callback that's // sent in the response, and check the error object stored within the // request pointer passed into the method Request *r = new Request(); RequestError *err = r->req_err; const char * uri_cstr = evhttp_request_uri(req); std::string uri(uri_cstr); r->req_addr = uri; // Get the data out of the evhttp request buffer try { size_t len = evbuffer_get_length(evhttp_request_get_input_buffer(req)); struct evbuffer *in_evb = evhttp_request_get_input_buffer(req); char *data = (char*)malloc(len); evbuffer_copyout(in_evb, data, len); // Put the data into the universal request struct std::string rdata(data, len); r->req_data = rdata; free(data); } catch (std::exception& e) { err->err_code = HTTP_BADREQUEST; err->err_message = e.what(); } // Determine the Request type if (req->type == 1) { // Get Request r->req_type = HTTP_GET; } else if (req->type == 2) { // Post Request r->req_type = HTTP_POST; } else if (req->type == 8) { // Put Request r->req_type = HTTP_PUT; } else if (req->type == 16) { // Delete Request r->req_type = HTTP_DELETE; } else { // Unknown Request type r->req_type = UNKNOWN; } std::string resp = ""; CallbackInterface cb; // Call the callback, store the return value in a string sent in a response try { auto search = callback_map.find(uri); if (search != callback_map.end()) { cb = callback_map[uri]; try { resp = (*cb)(r); } catch (std::exception& e) { err->err_code = HTTP_BADREQUEST; err->err_message = e.what(); } } else { // Not found, call default callback cb = callback_map["default"]; try { resp = (*cb)(r); } catch (std::exception& e) { err->err_code = HTTP_BADREQUEST; err->err_message = e.what(); } } } catch (std::exception& e) { err->err_code = HTTP_NOTFOUND; err->err_message = e.what(); } struct evbuffer *buf = evbuffer_new(); if (buf != NULL) { evbuffer_add_printf(buf, resp.c_str(), uri_cstr); // Send a success response if no error is detected if (r->req_err->err_code == NOERROR) { evhttp_send_reply(req, HTTP_OK, "OK", buf); } else { // Send a failure response evhttp_send_reply(req, err->err_code, err->err_message.c_str(), buf); } evbuffer_free(buf); } delete r; }
/** * http_handler - http handler */ void http_handler(struct evhttp_request *req, void *arg) { time_t timep; struct tm *m; struct stat info; struct evbuffer *buf; buf = evbuffer_new(); // 分析URL参数 char *decode_uri = strdup((char*) evhttp_request_uri(req)); decode_uri = evhttp_decode_uri(decode_uri); if (strcmp(decode_uri, "/") == 0) decode_uri = "/index.html"; sprintf(http_req_line.request_uri, "%s", decode_uri); // 返回给浏览器的头信息 evhttp_add_header(req->output_headers, "Server", "logadmin"); evhttp_add_header(req->output_headers, "Connection", "close"); // 取得请求时间 time(&timep); m = localtime(&timep); sprintf(http_req_line.request_time, "%4d-%02d-%02d %02d:%02d:%02d", (1900+m->tm_year), (1+m->tm_mon), m->tm_mday, m->tm_hour, m->tm_min, m->tm_sec); // 获取ACTION的值 struct evkeyvalq params; evhttp_parse_query(decode_uri, ¶ms); char *action = (char*)evhttp_find_header(¶ms, "action"); // 处理不同的ACTION if (action) { char *tmp = ""; evhttp_add_header(req->output_headers, "Content-Type", "text/html; charset=UTF-8"); if (strcmp(action, "loginfo") == 0) { char *loghost = (char*)evhttp_find_header(¶ms, "loghost"); char *logport = (char*)evhttp_find_header(¶ms, "logport"); char *logname = (char*)evhttp_find_header(¶ms, "logname"); char *pagenum = (char*)evhttp_find_header(¶ms, "pagenum"); int pnum = pagenum ? atoi(pagenum) : 1; int port = logport ? atoi(logport) : 8706; if (logname) { int psize = 20; char *logreg = (char*)evhttp_find_header(¶ms, "logreg"); if(!logreg || !strlen(logreg)){ logreg = ".*"; } char *cmd = joinstr("grep '%s' %s -c && grep '%s' %s | tail -n +%d | head -n %d", logreg, logname, logreg, logname, (pnum - 1) * psize, psize); if (!loghost || !strlen(loghost)) { tmp = exec_cmd(cmd); } else { sock_cmd(loghost, port, cmd, (char*)&tmp, MEM_SIZE); } } evbuffer_add_printf(buf, "%s", tmp); evhttp_send_reply(req, HTTP_OK, "OK", buf); } else if(strcmp(action, "loglist") == 0) { char *loghost = (char*)evhttp_find_header(¶ms, "loghost"); char *logport = (char*)evhttp_find_header(¶ms, "logport"); char *dirname = (char*)evhttp_find_header(¶ms, "dirname"); int port = logport ? atoi(logport) : 8706; if (dirname) { char *cmd = joinstr("ls -lF %s | awk '{print $9}'", dirname); if (!loghost || !strlen(loghost)) { tmp = exec_cmd(cmd); } else { sock_cmd(loghost, port, cmd, (char*)&tmp, MEM_SIZE); } } evbuffer_add_printf(buf, "%s", tmp); evhttp_send_reply(req, HTTP_OK, "OK", buf); } else if(strcmp(action, "logconf") == 0) { #ifndef __WIN32 GKeyFile* config = g_key_file_new(); GKeyFileFlags flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS; if (g_key_file_load_from_file(config, "./conf/logadmin.conf", flags, NULL)) { int i; gsize length; gchar** groups = g_key_file_get_groups(config, &length); for (i = 0; i < (int)length; i++) { gchar* host = g_key_file_get_string(config, groups[i], "host", NULL); gchar* path = g_key_file_get_string(config, groups[i], "path", NULL); gchar* port = g_key_file_get_string(config, groups[i], "port", NULL); if(!port || !strlen(port)) port = "8706"; if (host && path) tmp = joinstr("%s%s:%s;%s\n", tmp, host, port, path); } } g_key_file_free(config); #endif evbuffer_add_printf(buf, "%s", tmp); evhttp_send_reply(req, HTTP_OK, "OK", buf); } else { evbuffer_add_printf(buf, "<html><head><title>870 Error Action</title></head><body><h1 align=\"center\">870 Error Action</h1></body></html>"); evhttp_send_reply(req, 870, "Error Action", buf); } evbuffer_free(buf); return; } // 获取请求的服务器文件路径 char *dir_root = getcwd(NULL, 0); char *real_path = joinstr("%s/%s%s", dir_root, DOCUMENT_ROOT, http_req_line.request_uri); if (stat(real_path,&info) == -1) { evhttp_add_header(req->output_headers, "Content-Type", "text/html; charset=UTF-8"); if (errno == ENOENT) { evbuffer_add_printf(buf, "<html><head><title>404 Not Found</title></head><body><h1 align=\"center\">404 Not Found</h1></body></html>"); evhttp_send_reply(req, 404, "Not Found", buf); } else if(access(real_path,R_OK) < 0) { evbuffer_add_printf(buf, "<html><head><title>403 Not Found</title></head><body><h1 align=\"center\">403 Forbidden</h1></body></html>"); evhttp_send_reply(req, 403, "Forbidden", buf); } else { evbuffer_add_printf(buf, "<html><head><title>500 Server Error</title></head><body><h1 align=\"center\">500 Server Error</h1></body></html>"); evhttp_send_reply(req, 500, "Server Error", buf); } } else if(S_ISREG(info.st_mode)) { // 设置HEADER头并输出内容到浏览器 evhttp_add_header(req->output_headers, "Content-Type", joinstr("%s; charset=UTF-8", mime_type(real_path))); evbuffer_add_printf(buf, "%s", read_log(real_path)); evhttp_send_reply(req, HTTP_OK, "OK", buf); } // 内存释放 evbuffer_free(buf); free(dir_root); }