uriparse.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * uriparser - RFC 3986 URI parsing library
  3. *
  4. * Copyright (C) 2013, Radu Hociung <radu.uriparser@ohmi.org>
  5. * Copyright (C) 2013, Sebastian Pipping <sebastian@pipping.org>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer in the documentation and/or other materials
  19. * provided with the distribution.
  20. *
  21. * 3. Neither the name of the copyright holder nor the names of
  22. * its contributors may be used to endorse or promote products
  23. * derived from this software without specific prior written
  24. * permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  30. * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  31. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  33. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  37. * OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <uriparser/Uri.h>
  42. #ifdef _WIN32
  43. # include <winsock2.h>
  44. # include <ws2tcpip.h>
  45. # if defined(__MINGW32__) && \
  46. (!defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 3 || \
  47. _WIN32_WINNT < _WIN32_WINNT_VISTA)
  48. WINSOCK_API_LINKAGE const char * WSAAPI inet_ntop(
  49. int af, const void *src, char *dst, size_t size);
  50. # endif
  51. #else
  52. # include <sys/socket.h>
  53. # include <arpa/inet.h>
  54. # include <netinet/in.h>
  55. #endif
  56. #define RANGE(x) (int)((x).afterLast-(x).first), ((x).first)
  57. void usage(void) {
  58. printf("Usage: uriparse URI [..]\n");
  59. }
  60. int main(int argc, char *argv[]) {
  61. int retval = EXIT_SUCCESS;
  62. int i = 1;
  63. if (argc < 2) {
  64. usage();
  65. exit(1);
  66. }
  67. for (; i < argc; i++) {
  68. UriParserStateA state;
  69. UriUriA uri;
  70. char ipstr[INET6_ADDRSTRLEN];
  71. state.uri = &uri;
  72. printf("uri: %s\n", argv[i]);
  73. if (uriParseUriA(&state, argv[i]) != URI_SUCCESS) {
  74. /* Failure */
  75. printf("Failure: %s @ '%.18s' (#%lu)\n",
  76. (state.errorCode == URI_ERROR_SYNTAX)
  77. ? "syntax"
  78. : (state.errorCode == URI_ERROR_MALLOC)
  79. ? "not enough memory"
  80. : "liburiparser bug (please report)",
  81. state.errorPos,
  82. (long unsigned int)(state.errorPos - argv[i]));
  83. retval = EXIT_FAILURE;
  84. } else {
  85. if (uri.scheme.first) {
  86. printf("scheme: %.*s\n", RANGE(uri.scheme));
  87. }
  88. if (uri.userInfo.first) {
  89. printf("userInfo: %.*s\n", RANGE(uri.userInfo));
  90. }
  91. if (uri.hostText.first) {
  92. printf("hostText: %.*s\n", RANGE(uri.hostText));
  93. }
  94. if (uri.hostData.ip4) {
  95. inet_ntop(AF_INET, uri.hostData.ip4->data, ipstr, sizeof ipstr);
  96. printf("hostData.ip4: %s\n", ipstr);
  97. }
  98. if (uri.hostData.ip6) {
  99. inet_ntop(AF_INET6, uri.hostData.ip6->data, ipstr, sizeof ipstr);
  100. printf("hostData.ip6: %s\n", ipstr);
  101. }
  102. if (uri.portText.first) {
  103. printf("portText: %.*s\n", RANGE(uri.portText));
  104. }
  105. if (uri.pathHead) {
  106. const UriPathSegmentA * p = uri.pathHead;
  107. for (; p; p = p->next) {
  108. printf(" .. pathSeg: %.*s\n", RANGE(p->text));
  109. }
  110. }
  111. if (uri.query.first) {
  112. printf("query: %.*s\n", RANGE(uri.query));
  113. }
  114. if (uri.fragment.first) {
  115. printf("fragment: %.*s\n", RANGE(uri.fragment));
  116. }
  117. {
  118. const char * const absolutePathLabel = "absolutePath: ";
  119. printf("%s%s\n", absolutePathLabel,
  120. (uri.absolutePath == URI_TRUE) ? "true" : "false");
  121. if (uri.hostText.first != NULL) {
  122. printf("%*s%s\n", (int)strlen(absolutePathLabel), "",
  123. "(always false for URIs with host)");
  124. }
  125. }
  126. }
  127. printf("\n");
  128. uriFreeUriMembersA(&uri);
  129. }
  130. return retval;
  131. }