httpu.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #ifndef _PRO_HTTPU_H_
  2. #define _PRO_HTTPU_H_
  3. /*
  4. * Copyright (C) 2012-2013 by egnite GmbH
  5. *
  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 copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * \file pro/httpu.h
  38. * \brief HTTP over UDP library.
  39. */
  40. #include <cfg/http.h>
  41. #include <sys/socket.h>
  42. /*!
  43. * \addtogroup xgHTTPU HTTPU
  44. * \ingroup xgUserPro
  45. * \brief Hypertext transfer protocol over UDP library.
  46. */
  47. /*@{*/
  48. /*!
  49. * \brief Maximum datagram size.
  50. *
  51. * For each session a total number of 4 datagram buffers are allocated
  52. * from the heap.
  53. */
  54. #ifndef HTTPU_MAX_DATAGRAM_SIZE
  55. #define HTTPU_MAX_DATAGRAM_SIZE 508
  56. #endif
  57. /*!
  58. * \brief Maximum number of received header lines.
  59. *
  60. * Transmit header lines are only limited by
  61. * \ref HTTPU_MAX_DATAGRAM_SIZE.
  62. */
  63. #ifndef HTTPU_MAX_HEADER_LINES
  64. #define HTTPU_MAX_HEADER_LINES 16
  65. #endif
  66. /*! \brief HTTPU session structure type. */
  67. typedef struct _HTTPU_SESSION HTTPU_SESSION;
  68. /*! \brief HTTPU header structure type. */
  69. typedef struct _HTTPU_HEADER HTTPU_HEADER;
  70. /*! \brief HTTPU message structure type. */
  71. typedef struct _HTTPU_MESSAGE HTTPU_MESSAGE;
  72. /*! \brief HTTPU header structure. */
  73. struct _HTTPU_HEADER {
  74. /*! \brief Number of header lines. */
  75. int hdr_num;
  76. /*! \brief Header names. */
  77. char *hdr_name[HTTPU_MAX_HEADER_LINES];
  78. /*! \brief Header values. */
  79. char *hdr_value[HTTPU_MAX_HEADER_LINES];
  80. };
  81. /*! \brief HTTPU message structure. */
  82. struct _HTTPU_MESSAGE {
  83. /*! \brief Message content. */
  84. char msg_buff[HTTPU_MAX_DATAGRAM_SIZE + 1];
  85. /*! \brief Number of bytes in the buffer. */
  86. int msg_len;
  87. };
  88. /*! \brief HTTPU message structure. */
  89. struct _HTTPU_SESSION {
  90. /*! \brief UDP socket. */
  91. UDPSOCKET *s_sock;
  92. /*! \brief IP address of requesting client. */
  93. uint32_t s_reqip;
  94. /*! \brief Source port of a request. */
  95. unsigned short s_reqport;
  96. /*! \brief Buffer for outgoing datagrams. */
  97. HTTPU_MESSAGE s_sndbuf;
  98. /*! \brief Buffer for incoming datagrams. */
  99. HTTPU_MESSAGE s_rcvbuf;
  100. /*! \brief Headers of incoming datagrams. */
  101. HTTPU_HEADER s_rcvhdr;
  102. };
  103. /*!
  104. * \brief Create HTTPU session.
  105. *
  106. *
  107. *
  108. * \param port Server applications provide the local port number
  109. * with this parameter. Client applications may
  110. * pass zero to let the system select an available
  111. * port.
  112. *
  113. * \return Session descriptor of the newly created HTTPU session or
  114. * NULL in case of any failure.
  115. */
  116. extern HTTPU_SESSION *HttpuSessionCreate(uint16_t port);
  117. /*!
  118. * \brief Close HTTPU session.
  119. *
  120. * The memory occupied by the session is immediately released
  121. * after calling this function. The application must not use
  122. * the session descriptor after this call.
  123. *
  124. * \param s Session descriptor, obtained by a previous call to
  125. * HttpuSessionCreate().
  126. */
  127. extern void HttpuSessionDestroy(HTTPU_SESSION *s);
  128. /*!
  129. * \brief Receive HTTPU message.
  130. *
  131. * Note, that the total size of the HTTPU message is limited by
  132. * the configuration macro \ref HTTPU_MAX_DATAGRAM_SIZE.
  133. *
  134. * \param s Session descriptor, obtained by a previous call to
  135. * HttpuSessionCreate().
  136. * \param tmo Maximum number of milliseconds to wait.
  137. *
  138. * \return The number of bytes received, if successful. A negative
  139. * return value indicates an error, while zero indicates
  140. * a time out.
  141. */
  142. extern int HttpuReceive(HTTPU_SESSION *s, uint32_t tmo);
  143. /*!
  144. * \brief Send HTTPU message to a given destination.
  145. *
  146. * This function can be used by client applications to send an HTTPU
  147. * request.
  148. *
  149. * \param s Session descriptor, obtained by a previous call to
  150. * HttpuSessionCreate().
  151. * \param ip Destination IP address.
  152. * \param port Destination UDP port.
  153. *
  154. * \return 0 on success, -1 otherwise. The specific error code
  155. * can be retrieved by calling NutUdpError().
  156. */
  157. extern int HttpuSend(HTTPU_SESSION *s, uint32_t ip, uint16_t port);
  158. /*!
  159. * \brief Send HTTPU response.
  160. *
  161. * This function can be used by server applications to send an HTTPU
  162. * response.
  163. *
  164. * \param s Session descriptor, obtained by a previous call to
  165. * HttpuSessionCreate().
  166. *
  167. * \return 0 on success, -1 otherwise. The specific error code
  168. * can be retrieved by calling NutUdpError().
  169. */
  170. extern int HttpuRespond(HTTPU_SESSION *s);
  171. /*!
  172. * \brief Add header line to HTTPU message.
  173. *
  174. * This function is used to create the header of a HTTPU request or
  175. * response.
  176. *
  177. * Note, that the total size of an HTTPU message, header plus body, is
  178. * limited by the configuration macro \ref HTTPU_MAX_DATAGRAM_SIZE.
  179. *
  180. * \param s Session descriptor, obtained by a previous call to
  181. * HttpuSessionCreate().
  182. * \param name Name of the header. If set to NULL, then the header
  183. * value is used as the first line.
  184. * \param ... Any number of strings that will be concatenated to
  185. * form the header value. The last argument must be
  186. * a NULL pointer.
  187. *
  188. * \return Accumulated size of this line or -1 in case of buffer
  189. * overflow.
  190. */
  191. extern int HttpuAddHeader(HTTPU_SESSION *s, const char *name, ...);
  192. /*!
  193. * \brief Get HTTPU header value by name.
  194. *
  195. * This function can be used to query header values of incoming
  196. * messages.
  197. *
  198. * \param hdr Pointer to the session'request s header storage,
  199. * typically HTTPU_SESSION::s_rcvhdr.
  200. * \param name Name of the header.
  201. *
  202. * \return Pointer to the header value. If no header with the given
  203. * name is available, then a pointer to an empty string is
  204. * returned. Note, that the total number of HTTPU header
  205. * lines for incoming telegrams is limited by the
  206. * configuration macro \ref HTTPU_MAX_HEADER_LINES.
  207. */
  208. extern const char *HttpuGetHeader(const HTTPU_HEADER *hdr, const char *name);
  209. #endif