minihttptestserver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /* $Id: minihttptestserver.c,v 1.9 2012/04/06 14:03:58 nanard Exp $ */
  2. /* Project : miniUPnP
  3. * Author : Thomas Bernard
  4. * Copyright (c) 2011 Thomas Bernard
  5. * This software is subject to the conditions detailed in the
  6. * LICENCE file provided in this distribution.
  7. * */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <sys/wait.h>
  15. #include <arpa/inet.h>
  16. #include <netinet/in.h>
  17. #include <signal.h>
  18. #include <time.h>
  19. #define CRAP_LENGTH (2048)
  20. volatile int quit = 0;
  21. volatile int child_to_wait_for = 0;
  22. /**
  23. * signal handler for SIGCHLD (child status has changed)
  24. */
  25. void handle_signal_chld(int sig)
  26. {
  27. printf("handle_signal_chld(%d)\n", sig);
  28. ++child_to_wait_for;
  29. }
  30. /**
  31. * signal handler for SIGINT (CRTL C)
  32. */
  33. #if 0
  34. void handle_signal_int(int sig)
  35. {
  36. printf("handle_signal_int(%d)\n", sig);
  37. quit = 1;
  38. }
  39. #endif
  40. /**
  41. * build a text/plain content of the specified length
  42. */
  43. void build_content(char * p, int n)
  44. {
  45. char line_buffer[80];
  46. int k;
  47. int i = 0;
  48. while(n > 0) {
  49. k = snprintf(line_buffer, sizeof(line_buffer),
  50. "%04d_ABCDEFGHIJKL_This_line_is_64_bytes_long_ABCDEFGHIJKL_%04d\r\n",
  51. i, i);
  52. if(k != 64) {
  53. fprintf(stderr, "snprintf() returned %d in build_content()\n", k);
  54. }
  55. ++i;
  56. if(n >= 64) {
  57. memcpy(p, line_buffer, 64);
  58. p += 64;
  59. n -= 64;
  60. } else {
  61. memcpy(p, line_buffer, n);
  62. p += n;
  63. n = 0;
  64. }
  65. }
  66. }
  67. /**
  68. * build crappy content
  69. */
  70. void build_crap(char * p, int n)
  71. {
  72. static const char crap[] = "_CRAP_\r\n";
  73. int i;
  74. while(n > 0) {
  75. i = sizeof(crap) - 1;
  76. if(i > n)
  77. i = n;
  78. memcpy(p, crap, i);
  79. p += i;
  80. n -= i;
  81. }
  82. }
  83. /**
  84. * build chunked response.
  85. * return a malloc'ed buffer
  86. */
  87. char * build_chunked_response(int content_length, int * response_len) {
  88. char * response_buffer;
  89. char * content_buffer;
  90. int buffer_length;
  91. int i, n;
  92. /* allocate to have some margin */
  93. buffer_length = 256 + content_length + (content_length >> 4);
  94. response_buffer = malloc(buffer_length);
  95. *response_len = snprintf(response_buffer, buffer_length,
  96. "HTTP/1.1 200 OK\r\n"
  97. "Content-Type: text/plain\r\n"
  98. "Transfer-Encoding: chunked\r\n"
  99. "\r\n");
  100. /* build the content */
  101. content_buffer = malloc(content_length);
  102. build_content(content_buffer, content_length);
  103. /* chunk it */
  104. i = 0;
  105. while(i < content_length) {
  106. n = (rand() % 199) + 1;
  107. if(i + n > content_length) {
  108. n = content_length - i;
  109. }
  110. /* TODO : check buffer size ! */
  111. *response_len += snprintf(response_buffer + *response_len,
  112. buffer_length - *response_len,
  113. "%x\r\n", n);
  114. memcpy(response_buffer + *response_len, content_buffer + i, n);
  115. *response_len += n;
  116. i += n;
  117. response_buffer[(*response_len)++] = '\r';
  118. response_buffer[(*response_len)++] = '\n';
  119. }
  120. memcpy(response_buffer + *response_len, "0\r\n", 3);
  121. *response_len += 3;
  122. free(content_buffer);
  123. printf("resp_length=%d buffer_length=%d content_length=%d\n",
  124. *response_len, buffer_length, content_length);
  125. return response_buffer;
  126. }
  127. enum modes { MODE_INVALID, MODE_CHUNKED, MODE_ADDCRAP, MODE_NORMAL };
  128. const struct {
  129. const enum modes mode;
  130. const char * text;
  131. } modes_array[] = {
  132. {MODE_CHUNKED, "chunked"},
  133. {MODE_ADDCRAP, "addcrap"},
  134. {MODE_NORMAL, "normal"},
  135. {MODE_INVALID, NULL}
  136. };
  137. /**
  138. * write the response with random behaviour !
  139. */
  140. void send_response(int c, const char * buffer, int len)
  141. {
  142. int n;
  143. while(len > 0) {
  144. n = (rand() % 99) + 1;
  145. if(n > len)
  146. n = len;
  147. n = write(c, buffer, n);
  148. if(n < 0) {
  149. perror("write");
  150. return;
  151. } else {
  152. len -= n;
  153. buffer += n;
  154. }
  155. usleep(10000); /* 10ms */
  156. }
  157. }
  158. /**
  159. * handle the HTTP connection
  160. */
  161. void handle_http_connection(int c)
  162. {
  163. char request_buffer[2048];
  164. int request_len = 0;
  165. int headers_found = 0;
  166. int n, i;
  167. char request_method[16];
  168. char request_uri[256];
  169. char http_version[16];
  170. char * p;
  171. char * response_buffer;
  172. int response_len;
  173. enum modes mode;
  174. int content_length = 16*1024;
  175. /* read the request */
  176. while(request_len < sizeof(request_buffer) && !headers_found) {
  177. n = read(c,
  178. request_buffer + request_len,
  179. sizeof(request_buffer) - request_len);
  180. if(n < 0) {
  181. perror("read");
  182. return;
  183. } else if(n==0) {
  184. /* remote host closed the connection */
  185. break;
  186. } else {
  187. request_len += n;
  188. for(i = 0; i < request_len - 3; i++) {
  189. if(0 == memcmp(request_buffer + i, "\r\n\r\n", 4)) {
  190. /* found the end of headers */
  191. headers_found = 1;
  192. break;
  193. }
  194. }
  195. }
  196. }
  197. if(!headers_found) {
  198. /* error */
  199. return;
  200. }
  201. printf("headers :\n%.*s", request_len, request_buffer);
  202. /* the request have been received, now parse the request line */
  203. p = request_buffer;
  204. for(i = 0; i < sizeof(request_method) - 1; i++) {
  205. if(*p == ' ' || *p == '\r')
  206. break;
  207. request_method[i] = *p;
  208. ++p;
  209. }
  210. request_method[i] = '\0';
  211. while(*p == ' ')
  212. p++;
  213. for(i = 0; i < sizeof(request_uri) - 1; i++) {
  214. if(*p == ' ' || *p == '\r')
  215. break;
  216. request_uri[i] = *p;
  217. ++p;
  218. }
  219. request_uri[i] = '\0';
  220. while(*p == ' ')
  221. p++;
  222. for(i = 0; i < sizeof(http_version) - 1; i++) {
  223. if(*p == ' ' || *p == '\r')
  224. break;
  225. http_version[i] = *p;
  226. ++p;
  227. }
  228. http_version[i] = '\0';
  229. printf("Method = %s, URI = %s, %s\n",
  230. request_method, request_uri, http_version);
  231. /* check if the request method is allowed */
  232. if(0 != strcmp(request_method, "GET")) {
  233. const char response405[] = "HTTP/1.1 405 Method Not Allowed\r\n"
  234. "Allow: GET\r\n\r\n";
  235. const char * pc;
  236. /* 405 Method Not Allowed */
  237. /* The response MUST include an Allow header containing a list
  238. * of valid methods for the requested resource. */
  239. n = sizeof(response405) - 1;
  240. pc = response405;
  241. while(n > 0) {
  242. i = write(c, pc, n);
  243. if(i<0) {
  244. perror("write");
  245. return;
  246. }
  247. n -= i;
  248. pc += i;
  249. }
  250. return;
  251. }
  252. mode = MODE_INVALID;
  253. /* use the request URI to know what to do */
  254. for(i = 0; modes_array[i].mode != MODE_INVALID; i++) {
  255. if(strstr(request_uri, modes_array[i].text)) {
  256. mode = modes_array[i].mode; /* found */
  257. break;
  258. }
  259. }
  260. switch(mode) {
  261. case MODE_CHUNKED:
  262. response_buffer = build_chunked_response(content_length, &response_len);
  263. break;
  264. case MODE_ADDCRAP:
  265. response_len = content_length+256;
  266. response_buffer = malloc(response_len);
  267. n = snprintf(response_buffer, response_len,
  268. "HTTP/1.1 200 OK\r\n"
  269. "Server: minihttptestserver\r\n"
  270. "Content-Type: text/plain\r\n"
  271. "Content-Length: %d\r\n"
  272. "\r\n", content_length);
  273. response_len = content_length+n+CRAP_LENGTH;
  274. response_buffer = realloc(response_buffer, response_len);
  275. build_content(response_buffer + n, content_length);
  276. build_crap(response_buffer + n + content_length, CRAP_LENGTH);
  277. break;
  278. default:
  279. response_len = content_length+256;
  280. response_buffer = malloc(response_len);
  281. n = snprintf(response_buffer, response_len,
  282. "HTTP/1.1 200 OK\r\n"
  283. "Server: minihttptestserver\r\n"
  284. "Content-Type: text/plain\r\n"
  285. "\r\n");
  286. response_len = content_length+n;
  287. response_buffer = realloc(response_buffer, response_len);
  288. build_content(response_buffer + n, response_len - n);
  289. }
  290. if(response_buffer) {
  291. send_response(c, response_buffer, response_len);
  292. free(response_buffer);
  293. } else {
  294. /* Error 500 */
  295. }
  296. }
  297. /**
  298. */
  299. int main(int argc, char * * argv) {
  300. int ipv6 = 0;
  301. int s, c, i;
  302. unsigned short port = 0;
  303. struct sockaddr_storage server_addr;
  304. socklen_t server_addrlen;
  305. struct sockaddr_storage client_addr;
  306. socklen_t client_addrlen;
  307. pid_t pid;
  308. int child = 0;
  309. int status;
  310. const char * expected_file_name = NULL;
  311. for(i = 1; i < argc; i++) {
  312. if(argv[i][0] == '-') {
  313. switch(argv[i][1]) {
  314. case '6':
  315. ipv6 = 1;
  316. break;
  317. case 'e':
  318. /* write expected file ! */
  319. expected_file_name = argv[++i];
  320. break;
  321. case 'p':
  322. /* port */
  323. if(++i < argc) {
  324. port = (unsigned short)atoi(argv[i]);
  325. }
  326. break;
  327. default:
  328. fprintf(stderr, "unknown command line switch '%s'\n", argv[i]);
  329. }
  330. } else {
  331. fprintf(stderr, "unkown command line argument '%s'\n", argv[i]);
  332. }
  333. }
  334. srand(time(NULL));
  335. signal(SIGCHLD, handle_signal_chld);
  336. #if 0
  337. signal(SIGINT, handle_signal_int);
  338. #endif
  339. s = socket(ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0);
  340. if(s < 0) {
  341. perror("socket");
  342. return 1;
  343. }
  344. memset(&server_addr, 0, sizeof(struct sockaddr_storage));
  345. memset(&client_addr, 0, sizeof(struct sockaddr_storage));
  346. if(ipv6) {
  347. struct sockaddr_in6 * addr = (struct sockaddr_in6 *)&server_addr;
  348. addr->sin6_family = AF_INET6;
  349. addr->sin6_port = htons(port);
  350. addr->sin6_addr = in6addr_loopback;
  351. } else {
  352. struct sockaddr_in * addr = (struct sockaddr_in *)&server_addr;
  353. addr->sin_family = AF_INET;
  354. addr->sin_port = htons(port);
  355. addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  356. }
  357. if(bind(s, (struct sockaddr *)&server_addr,
  358. ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) < 0) {
  359. perror("bind");
  360. return 1;
  361. }
  362. if(listen(s, 5) < 0) {
  363. perror("listen");
  364. }
  365. if(port == 0) {
  366. server_addrlen = sizeof(struct sockaddr_storage);
  367. if(getsockname(s, (struct sockaddr *)&server_addr, &server_addrlen) < 0) {
  368. perror("getsockname");
  369. return 1;
  370. }
  371. if(ipv6) {
  372. struct sockaddr_in6 * addr = (struct sockaddr_in6 *)&server_addr;
  373. port = ntohs(addr->sin6_port);
  374. } else {
  375. struct sockaddr_in * addr = (struct sockaddr_in *)&server_addr;
  376. port = ntohs(addr->sin_port);
  377. }
  378. printf("Listening on port %hu\n", port);
  379. fflush(stdout);
  380. }
  381. /* write expected file */
  382. if(expected_file_name) {
  383. FILE * f;
  384. f = fopen(expected_file_name, "wb");
  385. if(f) {
  386. char * buffer;
  387. buffer = malloc(16*1024);
  388. build_content(buffer, 16*1024);
  389. i = fwrite(buffer, 1, 16*1024, f);
  390. if(i != 16*1024) {
  391. fprintf(stderr, "error writing to file %s : %dbytes written (out of %d)\n", expected_file_name, i, 16*1024);
  392. }
  393. free(buffer);
  394. fclose(f);
  395. } else {
  396. fprintf(stderr, "error opening file %s for writing\n", expected_file_name);
  397. }
  398. }
  399. /* fork() loop */
  400. while(!child && !quit) {
  401. while(child_to_wait_for > 0) {
  402. pid = wait(&status);
  403. if(pid < 0) {
  404. perror("wait");
  405. } else {
  406. printf("child(%d) terminated with status %d\n", pid, status);
  407. }
  408. --child_to_wait_for;
  409. }
  410. /* TODO : add a select() call in order to handle the case
  411. * when a signal is caught */
  412. client_addrlen = sizeof(struct sockaddr_storage);
  413. c = accept(s, (struct sockaddr *)&client_addr,
  414. &client_addrlen);
  415. if(c < 0) {
  416. perror("accept");
  417. return 1;
  418. }
  419. printf("accept...\n");
  420. pid = fork();
  421. if(pid < 0) {
  422. perror("fork");
  423. return 1;
  424. } else if(pid == 0) {
  425. /* child */
  426. child = 1;
  427. close(s);
  428. s = -1;
  429. handle_http_connection(c);
  430. }
  431. close(c);
  432. }
  433. if(s >= 0) {
  434. close(s);
  435. s = -1;
  436. }
  437. if(!child) {
  438. while(child_to_wait_for > 0) {
  439. pid = wait(&status);
  440. if(pid < 0) {
  441. perror("wait");
  442. } else {
  443. printf("child(%d) terminated with status %d\n", pid, status);
  444. }
  445. --child_to_wait_for;
  446. }
  447. printf("Bye...\n");
  448. }
  449. return 0;
  450. }
粤ICP备19079148号