int main( int argc, char** argv ) { if( argc < 4 ) { printf("usage: http_tester [GET,SET,PUT] url path\n"); return 1; } #if defined( _MSC_VER ) // Initialize Winsock WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { printf("WSAStartup failed: %d\n", iResult); return 1; } #endif // defined( _MSC_VER ) http_client_t c; http_client_result res; res = http_client_connect( &c, argv[2], 0, 0, 0 ); HTTP_ERR_CHECK( res ); if( strcmp( argv[1], "GET" ) == 0 ) { char* msgbody; size_t msgbody_size; res = http_client_get( c, argv[3], (void**)&msgbody, &msgbody_size, 0x0 ); HTTP_ERR_CHECK( res ); fprintf( stdout, "%.*s\n", (int)msgbody_size, msgbody ); free( msgbody ); } else if( strcmp( argv[1], "HEAD" ) == 0 ) { size_t msgbody_size; res = http_client_head( c, argv[3], &msgbody_size); HTTP_ERR_CHECK( res ); printf( "content size: %lu\n", msgbody_size ); } else if( strcmp( argv[1], "PUT" ) == 0 ) { res = http_client_put( c, argv[3], "apansson", 9 ); HTTP_ERR_CHECK( res ); } http_client_disconnect( c ); free( c ); return 0; }
int http_client_demo(void) { int idle_count = 0,command; rtp_net_init(); rtp_threads_init(); while ((command = prompt_for_command()) != COMMAND_QUIT) { HTTPManagedClient httpClient; if (http_client_init(&httpClient) < 0) return(-1); if (command == COMMAND_GET) http_client_get(&httpClient,0); else if (command == COMMAND_GETTO_FILE) http_client_get(&httpClient,1); else if (command == COMMAND_POST) http_client_post(&httpClient); else if (command == COMMAND_PUT) http_client_put(&httpClient); } rtp_net_exit(); return (0); }
int pmhttpClientFetch(http_client *cp, const char *url, char *body_buffer, size_t body_length, char *type_buffer, size_t type_length) { int sts = 0, redirected = 0; if (pmDebug & DBG_TRACE_HTTP) fprintf(stderr, "pmhttpClientFetch: %s\n", url); if (http_client_prepare(cp, url, body_buffer, body_length, type_buffer, type_length) != 0) return -1; while (redirected <= cp->max_redirect) { /* ensure we're connected to the server */ if ((sts = http_client_connect(cp)) < 0) return sts; /* make a GET request via the given URL */ if ((sts = http_client_get(cp)) < 0) return sts; if (sts == 1) /* server connection is lost */ continue; /* parse, extract body, handle redirect */ if ((sts = http_client_response(cp)) < 0) { if (sts == -EAGAIN) /* server closed */ continue; if (sts == -EMLINK) { /* http redirect */ redirected++; continue; } break; /* propogate errors */ } break; /* successful exchange */ } if (pmDebug & DBG_TRACE_HTTP) fprintf(stderr, "pmhttpClientFetch sts=%d\n", sts); return sts; }
void http_client_test2(void) { aio_worker_init(); pthread_t thread; bool running = true; thread_create(&thread, http_server_thread, &running); struct http_header_t headers[3]; headers[0].name = "User-Agent"; headers[0].value = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0"; headers[1].name = "Accept-Language"; headers[1].value = "en-US,en;q=0.5"; headers[2].name = "Connection"; headers[2].value = "keep-alive"; // block IO void *http = http_client_create("127.0.0.1", PORT, 1); assert(0 == http_client_get(http, "/", headers, sizeof(headers)/sizeof(headers[0]), http_client_test_onreply, NULL)); assert(0 == http_client_get(http, "/img/bdlogo.png", headers, sizeof(headers)/sizeof(headers[0]), http_client_test_onreply, NULL)); assert(0 == http_client_get(http, "/", NULL, 0, http_client_test_onreply, NULL)); http_client_destroy(http); // AIO int32_t ref = 0; http = http_client_create("127.0.0.1", PORT, 0); assert(0 == http_client_get(http, "/", headers, sizeof(headers)/sizeof(headers[0]), http_client_test_onreply, &ref)); while(1 != ref) system_sleep(1000); assert(0 == http_client_get(http, "/img/bdlogo.png", headers, sizeof(headers)/sizeof(headers[0]), http_client_test_onreply, &ref)); while(2 != ref) system_sleep(1000); assert(0 == http_client_get(http, "/", NULL, 0, http_client_test_onreply, &ref)); while(3 != ref) system_sleep(1000); http_client_destroy(http); running = false; thread_destroy(thread); aio_worker_cleanup(); }