//int http_post(char* url, char* data) void http_callback(void *data) { OPS("### @ API : Callback - Thread[%d]", pthread_self()); // struct data apiclient* apis = (void**)data; char *post; NEW(post, HTTP_POST_SIZE); strcpy(post, apis->data); pthread_mutex_unlock(&apis->mxlock); // apply memory char *host_name, *uri, *header, *next; NEW(host_name, 32); NEW(uri, 101); //get host name next = mid_value( apis->APICallback, "//", "/", host_name, 31 ); next = mid_value( next, "/", NULL, uri, 100 ); DEL(next); //connect int http_sock = qqsocket_create( TCP, NULL, 0 ); //printf(http_sock); if( http_sock <= 0 ) { DEL(host_name); DEL(uri); OPS("### @ API : Callback Connect Server Error."); return; } NEW( header, KB(4) ); qqsocket_connect( http_sock, host_name, 80 ); sprintf( header, "POST /%s HTTP/1.1\r\n" "HOST: %s\r\n" "User-Agent: QQRobot.APIServer.Agent\r\n" "X-Powered-By: %s\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "Content-Length: %d\r\n\r\n" "%s" , uri, host_name, "*****@*****.**", strlen(post), post ); DEL(post); DEL(host_name); DEL(uri); // Test DBG("\n###HTTP POST\n%s\n########\n", header); qqsocket_send( http_sock, (uchar*)header, strlen(header) ); DEL(header); char *response; NEW(response, KB(4)); qqsocket_recv( http_sock, (uchar*)response, KB(4) ); char *result; NEW(result, 1024); mid_value(response, "<result>", "</result>", result, 1024); OPS("### @ API : Callback Result : %s", result); DEL(response); DEL(result); qqsocket_close( http_sock ); //DEL(http_sock); pthread_detach(pthread_self()); }
//2009-2-7 9:21 Huang Guan //Download a file //http://verycode.qq.com/getimage?fromuin=942011793&touin=357339036&appt=1&flag=1 //GET HTTP/1.1 int http_request( int* http_sock, char* url, char* session, char* data, int* datalen ) { char host_name[32], uri[101], *header, *next, tmp[16]; int len, data_len; //get host name next = mid_value( url, "//", "/", host_name, 31 ); next = mid_value( next, "/", NULL, uri, 100 ); //connect *http_sock = qqsocket_create( TCP, NULL, 0 ); if( *http_sock <= 0 ) return -3; qqsocket_connect( *http_sock, host_name, 80 ); NEW( header, KB(4) ); sprintf( header, "GET %s HTTP/1.1\r\n\r\n", uri ); qqsocket_send( *http_sock, (uchar*)header, strlen(header) ); len = qqsocket_recv( *http_sock, (uchar*)header, KB(4) ); // puts( header ); if( len>0 ){ next = mid_value( header, "Content-Length: ", "\r\n", tmp, 15 ); data_len = atoi( tmp ); next = mid_value( header, "getqqsession:", "\r\n", session, 127 ); while( len < data_len ){ int ret = qqsocket_recv( *http_sock, (uchar*)(header + len), KB(4)-len ); if( ret > 0 ) len += ret; else break; } next = strstr( header, "\r\n\r\n" ) + 4; if( next-4>0 ){ if( *datalen > data_len ){ memcpy( (void*)data, (void*)next, data_len ); *datalen = data_len; }else{ DBG("datalen is too small."); } } } DEL( header ); qqsocket_close( *http_sock ); return 0; }
char* http_post(char *server, char *data) { // apply memory char *url, *host_name, *uri, *header, *next; NEW(url, 256); NEW(host_name, 64); NEW(uri, 128); //get host name strcpy(url, server); next = mid_value( url, "//", "/", host_name, 64 ); next = mid_value( next, "/", NULL, uri, 128 ); DEL(url); //DEL(next); //connect int http_sock = qqsocket_create( TCP, NULL, 0 ); //printf(http_sock); if( http_sock <= 0 ) { DEL(host_name); DEL(uri); return ""; } NEW( header, HTTP_POST_SIZE ); qqsocket_connect( http_sock, host_name, 80 ); sprintf( header, "POST /%s HTTP/1.1\r\n" "HOST: %s\r\n" "User-Agent: QQRobot.APIServer.Agent\r\n" "X-Powered-By: %s\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "Content-Length: %d\r\n\r\n" "%s" , uri, host_name, "*****@*****.**", strlen(data), data ); DEL(host_name); DEL(uri); // Test DBG("\n### @ DIY : HTTP POST REQUEST\n%s\n########\n", header); qqsocket_send( http_sock, (uchar*)header, strlen(header) ); DEL(header); char *response; NEW(response, HTTP_POST_SIZE); qqsocket_recv( http_sock, (uchar*)response, HTTP_POST_SIZE ); DBG("\n### @ DIY : HTTP POST RESPONSE\n%s\n########\n", response); qqsocket_close( http_sock ); //DEL(response); return response; }
void* packetmgr_recv( void* data ) { int ret; qqpacket* p; qqclient* qq = (qqclient*)data; qqpacketmgr* mgr = &qq->packetmgr; uchar* recv_buf; int pos; NEW( recv_buf, PACKET_SIZE ); p = packetmgr_new_packet( qq ); if( !p || !recv_buf ){ DBG("error: p=%x buf=%x", p, recv_buf ); DEL( p ); DEL( recv_buf ); return NULL; } pos = 0; while( qq->process != P_INIT ){ ret = qqsocket_recv( qq->socket, recv_buf, PACKET_SIZE-pos ); if( ret <= 0 ){ if( qq->process != P_INIT ){ // DBG("ret=%d", ret ); SLEEP(1); continue; }else{ break; } } pos += ret; //TCP only if( qq->network == TCP || qq->network == PROXY_HTTP ){ if ( pos > 2 ){ if( !qq->proxy_established && qq->network == PROXY_HTTP ){ if( strstr( (char*)recv_buf, "200" )!=NULL ){ DBG("proxy server reply ok!"); qq->proxy_established = 1; prot_login_touch( qq ); //手动添加到发送队列。 qqpacket* t; while( (t = loop_pop_from_tail( &mgr->temp_loop )) ) loop_push_to_head( &mgr->ready_loop, t ); //检查待发送包 check_ready_packets( qq ); }else{ DBG("proxy server reply failure!"); recv_buf[ret]=0; DBG( "%s", recv_buf ); qqclient_set_process( qq, P_ERROR ); } }else{ int len = ntohs(*(ushort*)recv_buf); if( pos >= len ) //a packet is O.K. { if( handle_packet( qq, p, recv_buf, len ) < 0 ) { pos = 0; continue; } pos -= len; //copy data to buf if( pos > 0 ){ memmove( recv_buf, recv_buf+len, pos ); } }else if( pos == PACKET_SIZE ){ DBG("error: pos: 0x%x ", pos ); pos = 0; } } } }else{ //UDP handle_packet( qq, p, recv_buf, ret ); pos = 0; } } DEL( recv_buf ); packetmgr_del_packet( mgr, p ); DBG("end."); return NULL; }