/src/tutorial/test url/parser.c.BACKUP.10308.c

https://github.com/guocongwudi/9315ass2 · C · 306 lines · 232 code · 42 blank · 32 comment · 122 complexity · e3b3107524f222c9c6a513cd1f2f1fec MD5 · raw file

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. typedef struct parsed_url {
  5. char *scheme;
  6. char *host;
  7. char *port;
  8. char *path;
  9. char *params;
  10. } Url;
  11. char *str_n_dup(char *, int);
  12. Url *parseURL(char *);
  13. Url *makeParsedURL();
  14. Url *freeParsedURL(Url *);
  15. char *str_n_dup(char *str, int n) {
  16. char *new = malloc(n + 1);
  17. if (new == NULL)
  18. return NULL;
  19. strncpy(new, str, n);
  20. new[n] = '\0';
  21. return new;
  22. }
  23. Url *parseURL(char *url) {
  24. char *c, *d;
  25. Url *purl = NULL;
  26. // trim trailing newline
  27. c = url;
  28. while (*c != '\0' && *c != '\n')
  29. c++;
  30. if (*c == '\n')
  31. *c = '\0';
  32. // create ParsedURL object
  33. if ((purl = makeParsedURL()) == NULL)
  34. return NULL;
  35. // start parse
  36. c = d = url;
  37. // find scheme component
  38. while (*d != '\0' && *d != ':')
  39. d++;
  40. // didn't find scheme
  41. if (*d == '\0')
  42. return freeParsedURL(purl);
  43. // copy scheme
  44. purl->scheme = str_n_dup(c, d - c);
  45. // must be "http" or "https"
  46. if (strcmp(purl->scheme, "http") != 0 && strcmp(purl->scheme, "https") != 0)
  47. return freeParsedURL(purl);
  48. // copy host
  49. if (*(d + 1) != '/' && *(d + 2) != '/')
  50. return freeParsedURL(purl);
  51. c = d = d + 3; // skip over '//'
  52. //skip other/
  53. while (*d == '/')
  54. d++;
  55. c = d;
  56. while (*d != '\0' && *d != ':' && *d != '/')
  57. d++;
  58. if (*d == '\0')
  59. return freeParsedURL(purl);
  60. purl->host = str_n_dup(c, d - c);
  61. // must contain at least one dot
  62. if (strchr(purl->host, '.') == NULL)
  63. return freeParsedURL(purl);
  64. // copy port, if any
  65. if (*d == ':') {
  66. c = d = d + 1; // skip over ':'
  67. while (*d != '\0' && *d != '/')
  68. d++;
  69. purl->port = str_n_dup(c, d - c);
  70. }
  71. // else {
  72. // if (strcmp(purl->scheme, "http") == 0)
  73. // purl->port = "80";
  74. //
  75. // else
  76. // purl->port = "403";
  77. //
  78. // }
  79. // **************merge*****************//
  80. // default port
  81. if (purl->port == NULL) {
  82. if (strcmp(purl->scheme, "http") == 0) {
  83. purl->port = malloc(3);
  84. strcpy(purl->port, "80");
  85. purl->port[2] = '\0';
  86. } else {
  87. purl->port = malloc(4);
  88. strcpy(purl->port, "443");
  89. purl->port[3] = '\0';
  90. }
  91. }
  92. // if url end with / and final component is not path, treat it as invalid
  93. if (*d == '/' && *d + 1 == '\0')
  94. return freeParsedURL(purl);
  95. // ****************merge************//
  96. // copy path, if any
  97. if (*d != '\0') {
  98. c = d = d + 1; // skip over '/'
  99. //skip other/
  100. while (*d == '/')
  101. d++;
  102. c = d;
  103. if (*d != '\0') {
  104. while (*d != '\0' && *d != '?')
  105. d++;
  106. purl->path = str_n_dup(c, d - c);
  107. }
  108. }
  109. // ********************merge**********************//
  110. // default path
  111. if (purl->path == NULL) {
  112. purl->path = malloc(11);
  113. strcpy(purl->path, "index.html");
  114. purl->path[10] = '\0';
  115. }
  116. // ****************merge**********************//
  117. // copy params, if any
  118. if (*d != '\0') {
  119. c = d = d + 1; // skip over '?'
  120. if (*d != '\0') {
  121. purl->params = strdup(c);
  122. }
  123. }
  124. int i;
  125. for (i = 0; i < strlen(purl->params); i++) {
  126. (purl->params)[i] = tolower((purl->params)[i]);
  127. }
  128. for (i = 0; i < strlen(purl->host); i++) {
  129. (purl->host)[i] = tolower((purl->host)[i]);
  130. }
  131. for (i = 0; i < strlen(purl->path); i++) {
  132. (purl->path)[i] = tolower((purl->path)[i]);
  133. }
  134. for (i = 0; i < strlen(purl->port); i++) {
  135. (purl->port)[i] = tolower((purl->port)[i]);
  136. }
  137. for (i = 0; i < strlen(purl->scheme); i++) {
  138. (purl->scheme)[i] = tolower((purl->scheme)[i]);
  139. }
  140. return purl;
  141. }
  142. Url *makeParsedURL() {
  143. Url *purl;
  144. if ((purl = malloc(sizeof(Url))) == NULL)
  145. return NULL;
  146. purl->scheme = NULL;
  147. purl->host = NULL;
  148. purl->port = NULL;
  149. purl->path = NULL;
  150. purl->params = NULL;
  151. return purl;
  152. }
  153. Url *freeParsedURL(Url *purl) {
  154. if (purl == NULL)
  155. return NULL;
  156. if (purl->scheme != NULL)
  157. free(purl->scheme);
  158. if (purl->host != NULL)
  159. free(purl->host);
  160. if (purl->port != NULL)
  161. free(purl->port);
  162. if (purl->path != NULL)
  163. free(purl->path);
  164. if (purl->params != NULL)
  165. free(purl->params);
  166. free(purl);
  167. return NULL;
  168. }
  169. int isUrlEqual(Url* a, Url* b) {
  170. int isEqual = 1; // 1 means equal
  171. if (strcmp(a->host, b->host) == 0 && strcmp(a->path, b->path) == 0 &&
  172. strcmp(a->params, b->params) == 0 && isEqual)
  173. isEqual = 1;
  174. else
  175. isEqual = 0;
  176. if (isEqual) {
  177. if ((strcmp(a->scheme, b->scheme) == 0 && strcmp(a->port, b->port) == 0)
  178. || ((strcmp(a->scheme, "http") == 0
  179. || strcmp(b->scheme, "https") == 0)
  180. && (strcmp(b->scheme, "http") == 0
  181. || strcmp(a->scheme, "https") == 0)
  182. && strcmp(a->port, b->port) == 0))
  183. isEqual = 1;
  184. } else
  185. isEqual = 0;
  186. return isEqual;
  187. }
  188. void main(void) {
  189. Url *url;
  190. Url *a;
  191. Url *b;
  192. char* line;
  193. char* line1;
  194. char* line2;
  195. line = "http://www.AAAA.com/song/play?ids=/song/playlist/id/7335983/type/3";
  196. line1 = "https://www.AAAA.com/play:80?ids=/song/playlist/id/7335983/3";
  197. line2 =
  198. "http://www.AAAA.com/song/play:80?ids=/song/playlist/id/7335983/type/4";
  199. url = parseURL(line);
  200. a = parseURL(line1);
  201. b = parseURL(line2);
  202. <<<<<<< HEAD
  203. result = (char *) malloc(url_len+5);
  204. result = strcat( result, url->scheme);
  205. result = strcat( result, "://");
  206. result = strcat( result, url->host);
  207. result = strcat( result, ":");
  208. result = strcat( result, "/" );
  209. if(url->path!=NULL)
  210. {
  211. result = strcat( result, url->path );
  212. =======
  213. int isEqual = isUrlEqual(a, b);
  214. int isLessThan = !isEqual;
  215. if (!isEqual) {
  216. char * aa = malloc(strlen(a->host) + strlen(a->path) + 1);
  217. char * bb = malloc(strlen(b->host) + strlen(b->path) + 1);
  218. strcpy(aa, a->host);
  219. strcpy(bb, b->host);
  220. strcat(aa, "/");
  221. strcat(bb, "/");
  222. strcat(aa, a->path);
  223. strcat(bb, b->path);
  224. printf("\n%s\n", aa);
  225. printf("\n%s\n", bb);
  226. int i = 0;
  227. for (i = strlen(aa) - 1; i >= 0; i--) {
  228. if (aa[i] != '/')
  229. continue;
  230. else {
  231. aa[i] = '\0';
  232. break;
  233. }
  234. >>>>>>> a492708f5d9a7bfe20d9c0113c2a53952f08d30e
  235. }
  236. for (i = strlen(bb) - 1; i >= 0; i--) {
  237. if (bb[i] != '/')
  238. continue;
  239. else {
  240. bb[i] = '\0';
  241. break;
  242. }
  243. }
  244. printf("\n%s\n", aa);
  245. printf("\n%s\n", bb);
  246. printf("--");
  247. printf("%d\n", strstr(aa, bb) == NULL);
  248. printf("%d\n", strstr(aa, bb));
  249. printf("%d\n", strstr(bb,aa));
  250. //if aa is less than bb, then aa.lengh < bb.lenth
  251. if (strlen(aa) >= strlen(bb))
  252. isLessThan = 0;
  253. else {
  254. // strstr() Return Value
  255. // A pointer to the first occurrence in str1 of any of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.
  256. if (strstr(aa, bb) == NULL)
  257. isLessThan = 1;
  258. else
  259. isLessThan = 0;
  260. }
  261. }
  262. printf("\n--------is less than--------\n");
  263. printf("%d\n", isLessThan);
  264. }