/* * should use evbuffer_free(evb); to free this evbuffer * */ static struct http_response * http_req(char * ip, int port, char * verb, char * path, char * data){ char port_str[6]; sprintf(port_str, "%d", port); int cs = client_socket(ip, port_str); char * request_buffer = alloca(100 + strlen(path)); // in stack do not need free char response_buffer[1024*8]; sprintf(request_buffer, "%s %s HTTP/1.0\r\nContent-Length: %d\r\n\r\n%s\r\n\r\n", verb, path, strlen(data), data); //sprintf(request_buffer, "GET %s HTTP/1.0\r\nUser-Agent: curl/7.19.7 (i486-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15\r\n\r\n", path); fprintf(stderr, "%s", request_buffer); int rst = tcptowrite(cs, request_buffer, strlen(request_buffer) , 30000); printf("tcptowrite rst: %d", rst); int readed = 0; struct evbuffer *evb = evbuffer_new(); /*evbuffer_read(evb, cs, 30);*/ while ((readed = tcptoread(cs, response_buffer, 1024*8, 30000)) > 0){ printf("readed len: %d \n", readed); evbuffer_add(evb, response_buffer, readed); } struct evbuffer_ptr evbptr = evbuffer_search(evb, "\r\n\r\n", 4, NULL); struct evbuffer *headers = evbuffer_new(); evbuffer_remove_buffer(evb, headers, evbptr.pos); /*evbuffer_drain(evb, evbptr.pos);*/ tcpclose(cs); int code = parse_response_status_code(headers); return http_response_new(code, headers, evb); }
static void pending_read(struct bufferevent *bev, void *pending_ptr) { struct evwspendingconn* pending = (struct evwspendingconn *)pending_ptr; struct evbuffer* input = bufferevent_get_input(pending->bev); struct evbuffer_ptr end = evbuffer_search(input, "\r\n\r\n", 4, NULL); size_t len = evbuffer_get_length(input); if (end.pos == -1) { if (len > MAX_HTTP_HEADER_SIZE) { remove_pending(pending); free_pending(pending); } return; // full request not yet found } unsigned char* data = evbuffer_pullup(input, len); char accept_key[29]; struct evwsconnlistener* levws = pending->levws; const char* subprotocol = NULL; if (evaluate_websocket_handshake((char*)data, len, levws->supported_subprotocols, accept_key, &subprotocol)) { remove_pending(pending); free_pending(pending); return; } evbuffer_drain(input, len); bufferevent_setcb(pending->bev, NULL, NULL, NULL, pending); struct evbuffer* output = bufferevent_get_output(pending->bev); evbuffer_add_printf(output, "HTTP/1.1 101 Switching Protocols\r\n" "Upgrade: websocket\r\n" "Connection: Upgrade\r\n" "Sec-WebSocket-Accept: %s\r\n", accept_key); if (subprotocol != NULL) { evbuffer_add_printf(output, "Sec-WebSocket-Protocol: %s\r\n\r\n", subprotocol); } else { evbuffer_add_printf(output, "\r\n"); } remove_pending(pending); struct evwsconn *wsconn = evwsconn_new(pending->bev, subprotocol); pending->bev = NULL; levws->cb(levws, wsconn, pending->address, pending->socklen, levws->user_data); free_pending(pending); }
static int readPadA( tr_handshake * handshake, struct evbuffer * inbuf ) { /* resynchronizing on HASH('req1',S) */ struct evbuffer_ptr ptr = evbuffer_search( inbuf, (const char*)handshake->myReq1, SHA_DIGEST_LENGTH, NULL ); if( ptr.pos != -1 ) /* match */ { evbuffer_drain( inbuf, ptr.pos ); dbgmsg( handshake, "found it... looking setting to awaiting_crypto_provide" ); setState( handshake, AWAITING_CRYPTO_PROVIDE ); return READ_NOW; } else { const size_t len = evbuffer_get_length( inbuf ); if( len > SHA_DIGEST_LENGTH ) evbuffer_drain( inbuf, len - SHA_DIGEST_LENGTH ); return READ_LATER; } }
long ion_stream_search_token(struct evbuffer * buffer, ion_stream_token * token) { struct evbuffer_ptr ptr_end; struct evbuffer_ptr ptr_start; struct evbuffer_ptr ptr_result; size_t current_size = evbuffer_get_length(buffer); size_t end = (size_t)token->offset + (size_t)token->length - 1; size_t length = (size_t)token->length; if(current_size == 0) { token->position = -1; return SUCCESS; } if(token->offset >= current_size || ZSTR_LEN(token->token) > current_size) { token->position = -1; return SUCCESS; } if(end >= current_size - 1) { // libevent bug? if <end> is last element - evbuffer_search_range can't find token length = 0; } if(evbuffer_ptr_set(buffer, &ptr_start, (size_t)token->offset, EVBUFFER_PTR_SET) == FAILURE) { return FAILURE; } if(length) { if(evbuffer_ptr_set(buffer, &ptr_end, end, EVBUFFER_PTR_SET) == FAILURE) { return FAILURE; } ptr_result = evbuffer_search_range(buffer, ZSTR_VAL(token->token), ZSTR_LEN(token->token), &ptr_start, &ptr_end); } else { ptr_result = evbuffer_search(buffer, ZSTR_VAL(token->token), ZSTR_LEN(token->token), &ptr_start); } if(token->length > 0 && current_size >= token->length) { token->flags |= ION_STREAM_TOKEN_LIMIT; } token->offset = (zend_long)(current_size - ZSTR_LEN(token->token) + 1); token->position = (long)ptr_result.pos; return SUCCESS; }
/*{{{ int*/ static gint64 bvalue_parse_int (struct evbuffer *in) { gchar tmp[1]; struct evbuffer_ptr pos; gchar *str; guint64 i; // remove "i" evbuffer_remove (in, tmp, 1); pos = evbuffer_search (in, "e", 1, NULL); str = g_new0 (gchar, pos.pos + 1); evbuffer_remove (in, str, pos.pos); str[pos.pos] = '\0'; i = atoll (str); g_free (str); // remove "e" evbuffer_remove (in, tmp, 1); return i; }
/*{{{ string */ static gchar *bvalue_parse_string (struct evbuffer *in, gint32 *len) { gchar *str; gchar *clen; struct evbuffer_ptr pos; gchar tmp[1]; pos = evbuffer_search (in, ":", 1, NULL); clen = g_new0 (gchar, pos.pos + 1); evbuffer_remove (in, clen, pos.pos); clen[pos.pos] = '\0'; *len = atoi (clen); g_free (clen); // remove ":" evbuffer_remove (in, tmp, 1); str = g_new0 (gchar, *len + 1); evbuffer_remove (in, str, *len); str[*len] = '\0'; return str; }
unsigned int Connection::searchInput( const char* what, unsigned int length ) { evbuffer_ptr found = evbuffer_search( m_input, what, length, NULL ); return found.pos; }