uri.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2012-2013 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. //#include <netdb.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <pro/uri.h>
  38. static const char *scheme_names[] = {
  39. "ftp://", "https://", "mailto:", "pop://", "snmp://", "telnet://", NULL
  40. };
  41. static const unsigned short default_ports[] = {
  42. 20, 443, 25, 110, 161, 23, 80
  43. };
  44. /* [name:][<user>[:<password>]@]<host>[:<port>][/<path>] */
  45. /*!
  46. * \brief Create a scheme structure from a URI.
  47. *
  48. * \param uri URI to parse. The expected format is
  49. * \code
  50. * [name:][<user>[:<password>]@]<host>[:<port>][/<path>]
  51. * \endcode
  52. *
  53. * \return Pointer to an allocated \ref URI_SCHEME "URI scheme"
  54. * structure. If this structure is no longer used, the caller
  55. * must call UriSchemeRelease(). In case of an error, NULL is
  56. * returned.
  57. */
  58. URI_SCHEME *UriSchemeSplit(const char *uri)
  59. {
  60. URI_SCHEME *schm = NULL;
  61. char *cp;
  62. if (uri && *uri) {
  63. /* Create a blank scheme structure. */
  64. schm = calloc(1, sizeof(*schm));
  65. if (schm) {
  66. int nidx;
  67. /* Determine the scheme name. */
  68. for (nidx = 0; scheme_names[nidx]; nidx++) {
  69. int len = strlen(scheme_names[nidx]);
  70. if (strncmp(uri, scheme_names[nidx], len) == 0) {
  71. uri += len;
  72. break;
  73. }
  74. }
  75. /* Create a local copy of the URI string. */
  76. schm->schm_uri = strdup(uri);
  77. if (schm->schm_uri) {
  78. /* Split the local copy. */
  79. schm->schm_host = schm->schm_uri;
  80. for (cp = schm->schm_uri; *cp; cp++) {
  81. if (*cp == ':') {
  82. *cp = '\0';
  83. schm->schm_port = cp + 1;
  84. }
  85. else if (*cp == '/') {
  86. *cp = 0;
  87. schm->schm_path = cp + 1;
  88. break;
  89. }
  90. else if (*cp == '@') {
  91. *cp = 0;
  92. schm->schm_user = schm->schm_host;
  93. schm->schm_pass = schm->schm_port;
  94. schm->schm_host = cp + 1;
  95. schm->schm_port = NULL;
  96. }
  97. }
  98. if (schm->schm_port) {
  99. schm->schm_portnum = (unsigned short) atoi(schm->schm_port);
  100. }
  101. else {
  102. schm->schm_portnum = default_ports[nidx];
  103. }
  104. return schm;
  105. }
  106. }
  107. UriSchemeRelease(schm);
  108. }
  109. return NULL;
  110. }
  111. /*!
  112. * \brief Release a HTTP scheme structure.
  113. *
  114. * \param schm Pointer to a \ref HttpSchemeParse "previously allocated"
  115. * HTTP scheme structure.
  116. */
  117. void UriSchemeRelease(URI_SCHEME *schm)
  118. {
  119. free(schm->schm_uri);
  120. free(schm);
  121. }