PageRenderTime 30ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 77 lines | 44 code | 13 blank | 20 comment | 9 complexity | 40460125c03cb4627e4c6e35f61e4ca1 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-url.c - HTTP client that sends bad URLs
  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 <string.h>
  20. #include "http-server.h"
  21. #include "util.h"
  22. int main(int argc, char ** argv) {
  23. #ifdef __MINGW32__
  24. WSADATA data;
  25. if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
  26. return 1;
  27. }
  28. #endif
  29. int result;
  30. if (argc < 3) {
  31. fprintf(stderr, "Usage: %s PORT URL\n", argv[0]);
  32. exit(EXIT_FAILURE);
  33. }
  34. uint16_t connect_port = atoi(argv[1]);
  35. char * url = argv[2];
  36. struct sockaddr_in a;
  37. a.sin_family = AF_INET;
  38. a.sin_port = htons(connect_port);
  39. a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  40. SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
  41. assert(s != INVALID_SOCKET);
  42. result = connect(s, (struct sockaddr *) &a, sizeof(a));
  43. assert(result == 0);
  44. /* send request */
  45. char * message;
  46. xasprintf(&message, "GET %s HTTP/1.1\r\nConnection: close\r\n\r\n", url);
  47. size_t message_length = strlen(message);
  48. ssize_t bytes_sent = send(s, message, message_length, 0);
  49. assert(bytes_sent == (ssize_t) message_length);
  50. /* read response */
  51. for (;;) {
  52. uint8_t buffer[8192];
  53. ssize_t bytes_read = recv(s, buffer, 8192, 0);
  54. assert(bytes_read >= 0);
  55. if (bytes_read == 0) {
  56. break;
  57. }
  58. }
  59. closesocket(s);
  60. return 0;
  61. }