static int event_handler(struct mg_event *event) { struct mg_request_info *ri = event->request_info; if (event->type == MG_REQUEST_BEGIN) { if (!strcmp(ri->uri, "/data")) { mg_printf(event->conn, "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n\r\n" "%s", fetch_data); close_connection(event->conn); return 1; } if (!strcmp(ri->uri, "/zerolen")) { char buf[100]; mg_printf(event->conn, "%s", "HTTP/1.0 200 OK\r\nContent-Length: 2\r\n\r\nok"); printf("[%d]\n", mg_read(event->conn, buf, sizeof(buf))); ASSERT(mg_read(event->conn, buf, sizeof(buf)) == 0); return 1; } if (!strcmp(ri->uri, "/upload")) { test_upload(event->conn, "lua_5.2.1.h", "./f1.txt"); test_upload(event->conn, "lsqlite3.c", "./f2.txt"); ASSERT(mg_upload(event->conn, ".", NULL, 0) == NULL); mg_printf(event->conn, "HTTP/1.0 200 OK\r\n" "Content-Type: text/plain\r\n\r\n" "%s", upload_ok_message); close_connection(event->conn); return 1; } } else if (event->type == MG_EVENT_LOG) { printf("%s\n", (const char *) event->event_param); } return 0; }
int main(int argc, char *argv[]) { int opt, num_thread; char server_url[URL_LENGTH_MAX], ext[UPLOAD_EXT_LENGTH_MAX]; double latency, speed, download_speed, upload_speed; int dsize, sindex; sindex = -1; num_thread = 4; dsize = INIT_DOWNLOAD_FILE_RESOLUTION; memset(server_url, 0, sizeof(server_url)); memset(ext, 0, sizeof(ext)); while ( (opt = getopt(argc, argv, "p:s:lh")) > 0) { switch (opt) { case 'p': if (atoi(optarg) > THREAD_NUM_MAX) { fprintf(stderr, "Only support %d threads meanwhile", THREAD_NUM_MAX); exit(-1); } num_thread = atoi(optarg); break; case 's': strncpy(server_url, optarg, URL_LENGTH_MAX); break; case 'l': break; case 'h': show_usage(argv); break; } } if (server_url[0] == 0) { printf("Retrieving speedtest.net configuration...\n"); get_client_info(&client); printf("Retrieving speedtest.net server list...\n"); get_closest_server(); printf("Testing from %s (%s)...\n", client.isp, client.ip); printf("Selecting best server based on ping...\n"); get_best_server(&sindex); sscanf(servers[sindex].url, "http://%[^/]/speedtest/upload.%4s", server_url, ext); printf("Bestest server: %s(%0.2fKM)\n", server_url, servers[sindex].distance); } /* Must initialize libcurl before any threads are started */ curl_global_init(CURL_GLOBAL_ALL); latency = test_latency(server_url); if (latency == DBL_MAX) exit(-1); printf("Server latency is %0.0fms\n", latency); speed = test_download(server_url, num_thread, dsize, 0); dsize = get_download_filename(speed, num_thread); fprintf(stderr, "Testing download speed"); download_speed = test_download(server_url, num_thread, dsize, 1); printf("Download speed: %0.2fMbps\n", ((download_speed*8)/(1024*1024))); if (ext[0] == 0 && get_upload_extension(server_url, ext) != OK) exit(-1); speed = test_upload(server_url, num_thread, speed, ext, 0); fprintf(stderr, "Testing upload speed"); upload_speed = test_upload(server_url, num_thread, speed*SPEEDTEST_TIME_MAX, ext, 1); printf("Upload speed: %0.2fMbps\n", ((upload_speed*8)/(1024*1024))); }