httpu.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <sys/nutdebug.h>
  35. #include <stdlib.h>
  36. #include <stdarg.h>
  37. #include <string.h>
  38. #include <pro/httpu.h>
  39. /* Use for local debugging only. Do NOT include into NUTDEBUG.
  40. #define DEBUG_HTTPU
  41. */
  42. #ifdef DEBUG_HTTPU
  43. #include <stdio.h>
  44. #endif
  45. /*!
  46. * \addtogroup xgHTTPU HTTPU
  47. *
  48. * \verbatim File version $Id$ \endverbatim
  49. */
  50. HTTPU_SESSION *HttpuSessionCreate(uint16_t port)
  51. {
  52. HTTPU_SESSION *s;
  53. s = calloc(1, sizeof(*s));
  54. if (s) {
  55. s->s_sock = NutUdpCreateSocket(port);
  56. if (s->s_sock == NULL) {
  57. free(s);
  58. s = NULL;
  59. } else {
  60. /* Enable UDP receive buffers. Otherwise the IP stack will
  61. only store the most recent datagram. */
  62. uint16_t rxbs = HTTPU_MAX_DATAGRAM_SIZE * 2;
  63. NutUdpSetSockOpt(s->s_sock, SO_RCVBUF, &rxbs, sizeof(rxbs));
  64. }
  65. }
  66. return s;
  67. }
  68. void HttpuSessionDestroy(HTTPU_SESSION * s)
  69. {
  70. NUTASSERT(s != NULL);
  71. NutUdpDestroySocket(s->s_sock);
  72. free(s);
  73. }
  74. int HttpuReceive(HTTPU_SESSION * s, uint32_t tmo)
  75. {
  76. char *lp;
  77. NUTASSERT(s != NULL);
  78. /* Set buffer pointer and receive datagram. */
  79. lp = s->s_rcvbuf.msg_buff;
  80. s->s_rcvbuf.msg_len =
  81. NutUdpReceiveFrom(s->s_sock, &s->s_reqip, (uint16_t *) &s->s_reqport, lp,
  82. sizeof(s->s_rcvbuf.msg_buff) - 1, tmo);
  83. if (s->s_rcvbuf.msg_len >= 0) {
  84. int idx;
  85. char *cp;
  86. char *nxt;
  87. *(lp + s->s_rcvbuf.msg_len) = '\0';
  88. #ifdef DEBUG_HTTPU
  89. printf("HTTPU Recv(%d)\n%s", s->s_rcvbuf.msg_len,
  90. s->s_rcvbuf.msg_buff);
  91. #endif
  92. /* Split the header into lines. */
  93. for (idx = 0; idx < HTTPU_MAX_HEADER_LINES && *lp; idx++, lp = nxt) {
  94. /* Search next carriage return. */
  95. nxt = strchr(lp, '\r');
  96. if (nxt == NULL) {
  97. /* No more lines available. */
  98. break;
  99. }
  100. /* Terminate line and skip line feed. */
  101. *nxt++ = '\0';
  102. if (*nxt == '\n') {
  103. nxt++;
  104. }
  105. if (idx == 0) {
  106. /* The first line is without name. */
  107. cp = lp;
  108. } else {
  109. /* Split name and value. */
  110. cp = strchr(lp, ':');
  111. if (cp == NULL) {
  112. /* This is not a header line. */
  113. break;
  114. }
  115. *cp++ = '\0';
  116. s->s_rcvhdr.hdr_name[idx] = lp;
  117. }
  118. /* Skip leading spaces in the value. */
  119. while (*cp == ' ') {
  120. cp++;
  121. }
  122. s->s_rcvhdr.hdr_value[idx] = cp;
  123. }
  124. s->s_rcvhdr.hdr_num = idx;
  125. }
  126. return s->s_rcvbuf.msg_len;
  127. }
  128. const char *HttpuGetHeader(const HTTPU_HEADER * hdr, const char *name)
  129. {
  130. int i;
  131. NUTASSERT(hdr != NULL);
  132. /* Find the line with the given name. Without name the first line
  133. is returned. */
  134. for (i = 0; i < hdr->hdr_num; i++) {
  135. if (name == NULL || strcasecmp(hdr->hdr_name[i], name) == 0) {
  136. return hdr->hdr_value[i];
  137. }
  138. }
  139. /* For non-existing headers return an empty string. */
  140. return "";
  141. }
  142. int HttpuAddHeader(HTTPU_SESSION * s, const char *name, ...)
  143. {
  144. va_list ap;
  145. int idx;
  146. char *str;
  147. int len;
  148. NUTASSERT(s != NULL);
  149. if (name) {
  150. /* Add header line, if name given. */
  151. len = strlen(name);
  152. idx = s->s_sndbuf.msg_len;
  153. memcpy(&s->s_sndbuf.msg_buff[idx], name, len);
  154. idx += len;
  155. s->s_sndbuf.msg_buff[idx++] = ':';
  156. s->s_sndbuf.msg_buff[idx++] = ' ';
  157. } else {
  158. /* Without name, create first header line. */
  159. memset(&s->s_sndbuf, 0, sizeof(s->s_sndbuf));
  160. idx = 0;
  161. }
  162. /* Add all arguments until a NULL pointer is reached. */
  163. va_start(ap, name);
  164. while ((str = va_arg(ap, char *)) != NULL) {
  165. len = strlen(str);
  166. if (len) {
  167. if (idx + len + 4 > sizeof(s->s_sndbuf.msg_buff)) {
  168. /* Buffer overflow. */
  169. idx = -1;
  170. break;
  171. }
  172. memcpy(&s->s_sndbuf.msg_buff[idx], str, len);
  173. idx += len;
  174. }
  175. }
  176. va_end(ap);
  177. if (idx >= 0) {
  178. /* Add end of line. */
  179. s->s_sndbuf.msg_buff[idx++] = '\r';
  180. s->s_sndbuf.msg_buff[idx++] = '\n';
  181. s->s_sndbuf.msg_len = idx;
  182. /* Add preventive end of header. */
  183. s->s_sndbuf.msg_buff[idx] = '\r';
  184. s->s_sndbuf.msg_buff[idx + 1] = '\n';
  185. }
  186. return idx;
  187. }
  188. int HttpuSend(HTTPU_SESSION * s, uint32_t ip, uint16_t port)
  189. {
  190. NUTASSERT(s != NULL);
  191. NUTASSERT(ip != 0);
  192. NUTASSERT(port != 0);
  193. #ifdef DEBUG_HTTPU
  194. printf("HTTPU Send(%d)\n%s", s->s_sndbuf.msg_len + 2, s->s_sndbuf.msg_buff);
  195. #endif
  196. /* Add 2 more bytes for the preventive end of header. */
  197. return NutUdpSendTo(s->s_sock, ip, port, s->s_sndbuf.msg_buff, s->s_sndbuf.msg_len + 2);
  198. }
  199. int HttpuRespond(HTTPU_SESSION * s)
  200. {
  201. NUTASSERT(s != NULL);
  202. /* Send response to the requester. */
  203. return HttpuSend(s, s->s_reqip, s->s_reqport);
  204. }