PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/tests/http-client-bad-body.c

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 98 lines | 64 code | 14 blank | 20 comment | 10 complexity | 7dd84467d7d570ccf02a49cb98f7688a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /*
  2. http-client-bad-body.c - HTTP client that outputs a bad body
  3. Copyright (C) 2008 siliconforks.com
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <config.h>
  17. #include <assert.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifdef HAVE_ARPA_INET_H
  22. #include <arpa/inet.h>
  23. #endif
  24. #ifdef HAVE_NETDB_H
  25. #include <netdb.h>
  26. #endif
  27. #ifdef HAVE_NETINET_IN_H
  28. #include <netinet/in.h>
  29. #endif
  30. #ifdef HAVE_SYS_SOCKET_H
  31. #include <sys/socket.h>
  32. #endif
  33. #include <unistd.h>
  34. #include "http-server.h"
  35. #include "util.h"
  36. int main(int argc, char ** argv) {
  37. #ifdef __MINGW32__
  38. WSADATA data;
  39. if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
  40. return 1;
  41. }
  42. #endif
  43. int result;
  44. if (argc < 3) {
  45. fprintf(stderr, "Usage: %s PORT URL\n", argv[0]);
  46. exit(EXIT_FAILURE);
  47. }
  48. uint16_t connect_port = atoi(argv[1]);
  49. char * url = argv[2];
  50. char * host;
  51. uint16_t port;
  52. char * abs_path;
  53. char * query;
  54. result = URL_parse(url, &host, &port, &abs_path, &query);
  55. assert(result == 0);
  56. struct sockaddr_in a;
  57. a.sin_family = AF_INET;
  58. a.sin_port = htons(connect_port);
  59. a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  60. SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
  61. assert(s != INVALID_SOCKET);
  62. result = connect(s, (struct sockaddr *) &a, sizeof(a));
  63. assert(result == 0);
  64. /* send request */
  65. char * message;
  66. xasprintf(&message, "POST %s HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\nHello\n", url);
  67. size_t message_length = strlen(message);
  68. ssize_t bytes_sent = send(s, message, message_length, 0);
  69. assert(bytes_sent == (ssize_t) message_length);
  70. /* read response */
  71. for (;;) {
  72. uint8_t buffer[8192];
  73. ssize_t bytes_read = recv(s, buffer, 8192, 0);
  74. assert(bytes_read >= 0);
  75. if (bytes_read == 0) {
  76. break;
  77. }
  78. }
  79. closesocket(s);
  80. return 0;
  81. }