bool CHttpParser::setHttpRequest(struct evhttp_request *req) { if (NULL != m_pEventBuf) { evbuffer_free(m_pEventBuf); m_pEventBuf = NULL; } m_strPostMsg.clear(); m_strQuery.clear(); m_pEventBuf = evbuffer_new(); if (NULL == m_pEventBuf) { Q_Printf("%s", "evbuffer_new error."); return false; } m_Req = req; struct evbuffer *pBuf = evhttp_request_get_input_buffer(m_Req); size_t iLens = evbuffer_get_length(pBuf); if (iLens > 0) { m_strPostMsg.append((const char *)evbuffer_pullup(pBuf, (ev_ssize_t)iLens), iLens); evbuffer_drain(pBuf, iLens); } const struct evhttp_uri *pUri = evhttp_request_get_evhttp_uri(m_Req); const char *pszQuery = evhttp_uri_get_query(pUri); m_strQuery = (NULL == pszQuery ? "" : pszQuery); return true; }
void xBlog::HttpParseURL(struct evhttp_request *req, struct evkeyvalq *evMyheader) { const char *xBlog_query; xBlog_query = evhttp_uri_get_query(evhttp_request_get_evhttp_uri(req)); evhttp_parse_query_str(xBlog_query, evMyheader); }
/* * Get request URI scheme * @return [String] http or https * @return [nil] */ static VALUE t_get_uri_scheme(VALUE self) { Libevent_HttpRequest *http_request; const struct evhttp_uri *ev_uri; const char* scheme; Data_Get_Struct(self, Libevent_HttpRequest, http_request); ev_uri = evhttp_request_get_evhttp_uri(http_request->ev_request); scheme = evhttp_uri_get_scheme(ev_uri); return(scheme ? rb_str_new2(scheme) : Qnil); }
static void stat_srv_on_gen_cb (struct evhttp_request *req, void *ctx) { StatSrv *stat_srv = (StatSrv *) ctx; const gchar *query; query = evhttp_uri_get_query (evhttp_request_get_evhttp_uri (req)); LOG_debug (STAT_LOG, "Unhandled request to: %s", query); (void) stat_srv; evhttp_send_reply (req, HTTP_NOTFOUND, "Not found", NULL); }
static void parseRequest(RequestInfo& requestInfo) { const struct evhttp_uri* uri = evhttp_request_get_evhttp_uri(requestInfo.req); /*const*/ struct evhttp_connection* conn = evhttp_request_get_connection(requestInfo.req); requestInfo.method = evhttp_request_get_command(requestInfo.req); requestInfo.uriHost = evhttp_uri_get_host(uri); requestInfo.uriPath = evhttp_uri_get_path(uri); requestInfo.uriQuery = evhttp_uri_get_query(uri); requestInfo.uriScheme = evhttp_uri_get_scheme(uri); requestInfo.requestHeaders = evhttp_request_get_input_headers(requestInfo.req); requestInfo.requestBody = evhttp_request_get_input_buffer(requestInfo.req); evhttp_connection_get_peer(conn, &requestInfo.remoteAddress, (ev_uint16_t*) &requestInfo.remotePort); }
void get_master_pgn(struct evhttp_request *req, void *context) { if (evhttp_request_get_command(req) != EVHTTP_REQ_GET) { evhttp_send_error(req, HTTP_BADMETHOD, "Method Not Allowed"); return; } const struct evhttp_uri *uri = evhttp_request_get_evhttp_uri(req); if (!uri) { puts("evhttp_request_get_evhttp_uri failed"); return; } const char *path = evhttp_uri_get_path(uri); if (!path) { puts("evhttp_uri_get_path failed"); return; } char game_id[9] = {}; int end; if (1 != sscanf(path, "/master/pgn/%8s%n", game_id, &end) || strlen(game_id) != 8 || strlen(path) != end) { evhttp_send_error(req, HTTP_NOTFOUND, "Not Found"); return; } size_t pgn_size; char *pgn = kcdbget(master_pgn_db, game_id, 8, &pgn_size); if (!pgn) { evhttp_send_error(req, HTTP_NOTFOUND, "Master PGN Not Found"); return; } struct evbuffer *res = evbuffer_new(); if (!res) { puts("could not allocate response buffer"); abort(); } struct evkeyvalq *headers = evhttp_request_get_output_headers(req); evhttp_add_header(headers, "Content-Type", "application/vnd.chess-pgn; charset=utf-8"); evbuffer_add(res, pgn, pgn_size); kcfree(pgn); evhttp_send_reply(req, HTTP_OK, "OK", res); evbuffer_free(res); }
static void http_counterdata(struct evhttp_request *r, const char *uri) { char *json; const char *query; query = evhttp_uri_get_query(evhttp_request_get_evhttp_uri(r)); hlog(LOG_DEBUG, "counterdata query: %s", query); json = cdata_json_string(query); if (!json) { evhttp_send_error(r, HTTP_BADREQUEST, "Bad request, no such counter"); return; } struct evkeyvalq *headers = evhttp_request_get_output_headers(r); http_header_base(headers, tick); evhttp_add_header(headers, "Content-Type", "application/json; charset=UTF-8"); evhttp_add_header(headers, "Cache-Control", "max-age=58"); http_send_reply_ok(r, headers, json, strlen(json), 1); hfree(json); }
void tracker(struct evhttp_request *req, struct config *confg){ char * info_hash=0x00, * client_ip=0x00, * event=0x00; int client_port=0, left = 0, compact = 0, numwant = 0, peer_worth = 0, uploaded = 0, downloaded = 0; ev_uint16_t scoket_client_port=0x00; uint32_t client_ip_addr=0; //evhttp is strange sometimes... struct evkeyvalq GET; const struct evhttp_uri *uri = evhttp_request_get_evhttp_uri(req); char *query = evhttp_uri_get_query(uri); evhttp_parse_query_str(query, &GET); info_hash=evhttp_find_header(&GET, "info_hash"); if(info_hash){ event=evhttp_find_header(&GET, "event"); left = getInt(&GET,"left"); //Only return a peer list if the client needs one. //So the client must have something left to download and not completed or stopped. //If the event is empty or non-present, return a list of peers. if(left && (!event || event[0] == 0x00 || (strcmp(event, "completed") == 0 && strcmp(event, "stopped") == 0))){ //Build a list of peers for the response: compact = getInt(&GET,"compact"); numwant = getInt(&GET,"numwant"); view_peer_list(confg, req, info_hash, numwant, compact); } client_ip = evhttp_find_header(&GET, "Ip"); if(client_ip){ client_ip_addr=inet_addr(client_ip); }else{ //for compatibility, try a non-standard case client_ip = evhttp_find_header(&GET, "ip"); if(client_ip){ client_ip_addr=inet_addr(client_ip); } } if(client_ip_addr<=0){ //Looks like we got an invalid ip address as a GET param, recovering... evhttp_connection_get_peer(req->evcon, &client_ip, &scoket_client_port); client_ip_addr=inet_addr(client_ip); } client_port = getInt(&GET,"port"); //port 0 is valid... and is used by no one, ever. if(client_port > 0 && client_port < 65536 && client_port != 80 && client_port != 443){ uploaded = getInt(&GET,"uploaded"); //Process event: if(event){ if(strcmp(event, "stopped") == 0 ){ peer_worth=-1; }else if(strcmp(event, "completed") == 0){ //This peer has every chunk, they are valuable peer_worth=2; }else if(strcmp(event, "started") == 0){ //This peer has nothing peer_worth=0; }else{ //Probably better than nothing. peer_worth=1; } }else{ peer_worth=1; } downloaded = getInt(&GET,"downloaded"); if(uploaded>downloaded){ //This peer is healthy! (Or lying...) peer_worth++; } if(peer_worth >= 0){ //Add the peer to the db, The recorded peer_worth is from 0-3 control_add_peer(confg, peer_worth, client_ip_addr, client_port, info_hash); }else if(peer_worth < 0){ //This peer is worth less than control_remove_peer(confg, client_ip_addr, client_port, info_hash); } }else{ //todo error invalid port } }else{ //todo error; no info_hash } }
size_t i; for (i = 0; i < uri_length; ++i) {
//Main int main(int argc, char *argv[]) { //Get args if (argc < 2) { fprintf(stderr, "Usage: %s <snapshot_file>\n", argv[0]); return 1; } snap_path = argv[1]; //Allocate memory fprintf(stderr, "Allocating arena with size %.2f MBytes ...\n", (float)m / CHAR_BIT / MEGA); Bloom = malloc( (m + ( CHAR_BIT - 1)) / CHAR_BIT ); // Ceil byte length: bytes = bits + 7 / 8 //Load or create snapshot file if (!access(snap_path, F_OK)) { fputs("Loading snapshot...\n", stderr); if (LoadSnap(Bloom, snap_path)) { fputs("Unable to load snapshot!\n", stderr); return -1; } fputs("Snapshot loaded.\n", stderr); } else { fputs("Initializing new file storage...\n", stderr); size_t shouldwrite = (m + (CHAR_BIT - 1)) / CHAR_BIT; memset(Bloom, 0, shouldwrite); if (SaveSnap(Bloom, snap_path)) { fputs("Unable to save initial snapshot!\n", stderr); return -1; } fputs("Initial snapshot written.\n", stderr); } void OnReq(struct evhttp_request *req, void *arg) { struct evbuffer *OutBuf = evhttp_request_get_output_buffer(req); if (!OutBuf) { evhttp_send_reply(req, HTTP_BADREQUEST, "Bad Request", OutBuf); return; } struct evkeyvalq *Headers = evhttp_request_get_output_headers(req); if (!Headers) { evhttp_send_reply(req, HTTP_INTERNAL, "Internal Error", OutBuf); return; } const struct evhttp_uri *HTTPURI = evhttp_request_get_evhttp_uri(req); if (!HTTPURI) { evhttp_send_reply(req, HTTP_BADREQUEST, "Bad Request", OutBuf); return; } const char *path = evhttp_uri_get_path(HTTPURI); if (!path) { evhttp_send_reply(req, HTTP_BADREQUEST, "Bad Request", OutBuf); } const char *query_string = evhttp_uri_get_query(HTTPURI); if (!query_string) { evhttp_send_reply(req, HTTP_BADREQUEST, "Element Required", OutBuf); return; } struct evkeyvalq params; evhttp_parse_query_str(query_string, ¶ms); const char *element = evhttp_find_header(¶ms, "e"); if (!element) { evhttp_clear_headers(¶ms); evhttp_send_reply(req, HTTP_BADREQUEST, "Element Required", OutBuf); return; } int i; const char* (*Operation)(bloom_cell *, size_t []) = NULL; for (i=0; i< sizeof HandlerTable/ sizeof HandlerTable[0] ; i++) if (strncmp(HandlerTable[i][0], path, STR_MAX) == 0) { Operation = HandlerTable[i][1]; break; } if (!Operation) { evhttp_clear_headers(¶ms); evhttp_send_reply(req, HTTP_NOTFOUND, "Not Found", OutBuf); return; } const char *response = Operation(Bloom, Hashes(element)); evhttp_add_header(Headers, MIME_TYPE); evbuffer_add_printf(OutBuf, response); evhttp_send_reply(req, HTTP_OK, "OK", OutBuf); evhttp_clear_headers(¶ms); }; if (!(base = event_base_new())) crash("Couldn't create an event_base: exiting\n", -1); if (!(http = evhttp_new(base))) crash("Couldn't create evhttp. Exiting.\n", -1); evhttp_set_gencb(http, OnReq, NULL); if (!(handle = evhttp_bind_socket_with_handle(http, BIND_ADDRESS, BIND_PORT))) crash("couldn't bind to port 8888. Exiting.\n", -1); if (signal(SIGINT, term_handler) == SIG_ERR) crash("Unable to set SIGINT handler!", -1); if (signal(SIGTERM, term_handler) == SIG_ERR) crash("Unable to set SIGTERM handler!", -1); if (signal(SIGCHLD, child_collector) == SIG_ERR) crash("Unable to set SIGCHLD handler!", -1); //This signal handled by event loop in order to avoid malloc deadlock if ((dump_event = evsignal_new(base, SIGUSR1, dump_handler, NULL)) == NULL) crash("Unable to create SIGUSR1 handler!", -1); else if (event_add(dump_event, NULL) == -1) crash("Unable to add SIGUSR1 handler!", -1); if (event_base_dispatch(base) == -1) crash("Failed to run message loop.\n", -1); fputs("Exiting...\n", stderr); SaveSnap(Bloom, snap_path); evhttp_del_accept_socket(http, handle); evhttp_free(http); event_free(dump_event); event_base_free(base); free(Bloom); return 0; }