PageRenderTime 17ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 156 lines | 121 code | 12 blank | 23 comment | 42 complexity | f0a620ab49116a316acfd3650ca33f05 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /*
  2. http-url.c - URL parsing routines
  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 "http-server.h"
  18. #include <ctype.h>
  19. #include <string.h>
  20. #include "util.h"
  21. int URL_parse_host_and_port(const char * s, char ** host, uint16_t * port) {
  22. char * colon = strchr(s, ':');
  23. if (colon == NULL) {
  24. *host = xstrdup(s);
  25. *port = 80;
  26. }
  27. else {
  28. if (*(colon + 1) == '\0') {
  29. *port = 80;
  30. }
  31. else {
  32. char * end;
  33. unsigned long p = strtoul(colon + 1, &end, 10);
  34. if (*end == '\0') {
  35. if (p > UINT16_MAX) {
  36. return -1;
  37. }
  38. else {
  39. *port = p;
  40. }
  41. }
  42. else {
  43. return -1;
  44. }
  45. }
  46. *host = xstrndup(s, colon - s);
  47. }
  48. return 0;
  49. }
  50. int URL_parse_abs_path_and_query(const char * s, char ** abs_path, char ** query) {
  51. if (*s == '\0') {
  52. *abs_path = xstrdup("/");
  53. *query = NULL;
  54. }
  55. else if (*s == '?') {
  56. *abs_path = xstrdup("/");
  57. *query = xstrdup(s + 1);
  58. }
  59. else if (*s == '/') {
  60. char * question = strchr(s, '?');
  61. if (question == NULL) {
  62. *abs_path = xstrdup(s);
  63. *query = NULL;
  64. }
  65. else {
  66. *abs_path = xstrndup(s, question - s);
  67. *query = xstrdup(question + 1);
  68. }
  69. }
  70. else {
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. int URL_parse(const char * url, char ** host, uint16_t * port, char ** abs_path, char ** query) {
  76. /* check for invalid characters */
  77. for (const char * p = url; *p != '\0'; p++) {
  78. switch (*p) {
  79. case ';':
  80. case '/':
  81. case '?':
  82. case ':':
  83. case '@':
  84. case '&':
  85. case '=':
  86. case '+':
  87. case '$':
  88. case ',':
  89. case '-':
  90. case '_':
  91. case '.':
  92. case '!':
  93. case '~':
  94. case '*':
  95. case '\'':
  96. case '(':
  97. case ')':
  98. case '%':
  99. break;
  100. default:
  101. if (! isalnum(*p)) {
  102. return -1;
  103. }
  104. break;
  105. }
  106. }
  107. int result;
  108. if (strncasecmp(url, "http://", 7) == 0) {
  109. /* absoluteURI */
  110. /* advance past the http:// */
  111. const char * authority_start = url + 7;
  112. /* look for a slash or question mark */
  113. const char * p;
  114. for (p = authority_start; *p != '/' && *p != '?' && *p != '\0'; p++) {
  115. ;
  116. }
  117. char * host_and_port = xstrndup(authority_start, p - authority_start);
  118. result = URL_parse_host_and_port(host_and_port, host, port);
  119. free(host_and_port);
  120. if (result != 0) {
  121. return result;
  122. }
  123. result = URL_parse_abs_path_and_query(p, abs_path, query);
  124. if (result != 0) {
  125. free(*host);
  126. *host = NULL;
  127. return result;
  128. }
  129. }
  130. else {
  131. /* abs_path */
  132. *host = NULL;
  133. *port = 80;
  134. result = URL_parse_abs_path_and_query(url, abs_path, query);
  135. if (result != 0) {
  136. return result;
  137. }
  138. }
  139. result = 0;
  140. return result;
  141. }