http_client.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @file
  3. * HTTP client
  4. */
  5. /*
  6. * Copyright (c) 2018 Simon Goldschmidt <goldsimon@gmx.de>
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * This file is part of the lwIP TCP/IP stack.
  32. *
  33. * Author: Simon Goldschmidt <goldsimon@gmx.de>
  34. *
  35. */
  36. #ifndef LWIP_HDR_APPS_HTTP_CLIENT_H
  37. #define LWIP_HDR_APPS_HTTP_CLIENT_H
  38. #include "lwip/opt.h"
  39. #include "lwip/ip_addr.h"
  40. #include "lwip/err.h"
  41. #include "lwip/altcp.h"
  42. #include "lwip/prot/iana.h"
  43. #include "lwip/pbuf.h"
  44. #if LWIP_TCP && LWIP_CALLBACK_API
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @ingroup httpc
  50. * HTTPC_HAVE_FILE_IO: define this to 1 to have functions downloading directly
  51. * to disk via fopen/fwrite.
  52. * These functions are example implementations of the interface only.
  53. */
  54. #ifndef LWIP_HTTPC_HAVE_FILE_IO
  55. #define LWIP_HTTPC_HAVE_FILE_IO 0
  56. #endif
  57. /**
  58. * @ingroup httpc
  59. * The default TCP port used for HTTP
  60. */
  61. #define HTTP_DEFAULT_PORT LWIP_IANA_PORT_HTTP
  62. /**
  63. * @ingroup httpc
  64. * HTTP client result codes
  65. */
  66. typedef enum ehttpc_result {
  67. /** File successfully received */
  68. HTTPC_RESULT_OK = 0,
  69. /** Unknown error */
  70. HTTPC_RESULT_ERR_UNKNOWN = 1,
  71. /** Connection to server failed */
  72. HTTPC_RESULT_ERR_CONNECT = 2,
  73. /** Failed to resolve server hostname */
  74. HTTPC_RESULT_ERR_HOSTNAME = 3,
  75. /** Connection unexpectedly closed by remote server */
  76. HTTPC_RESULT_ERR_CLOSED = 4,
  77. /** Connection timed out (server didn't respond in time) */
  78. HTTPC_RESULT_ERR_TIMEOUT = 5,
  79. /** Server responded with an error code */
  80. HTTPC_RESULT_ERR_SVR_RESP = 6,
  81. /** Local memory error */
  82. HTTPC_RESULT_ERR_MEM = 7,
  83. /** Local abort */
  84. HTTPC_RESULT_LOCAL_ABORT = 8,
  85. /** Content length mismatch */
  86. HTTPC_RESULT_ERR_CONTENT_LEN = 9
  87. } httpc_result_t;
  88. typedef struct _httpc_state httpc_state_t;
  89. /**
  90. * @ingroup httpc
  91. * Prototype of a http client callback function
  92. *
  93. * @param arg argument specified when initiating the request
  94. * @param httpc_result result of the http transfer (see enum httpc_result_t)
  95. * @param rx_content_len number of bytes received (without headers)
  96. * @param srv_res this contains the http status code received (if any)
  97. * @param err an error returned by internal lwip functions, can help to specify
  98. * the source of the error but must not necessarily be != ERR_OK
  99. */
  100. typedef void (*httpc_result_fn)(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, err_t err);
  101. /**
  102. * @ingroup httpc
  103. * Prototype of http client callback: called when the headers are received
  104. *
  105. * @param connection http client connection
  106. * @param arg argument specified when initiating the request
  107. * @param hdr header pbuf(s) (may contain data also)
  108. * @param hdr_len length of the headers in 'hdr'
  109. * @param content_len content length as received in the headers (-1 if not received)
  110. * @return if != ERR_OK is returned, the connection is aborted
  111. */
  112. typedef err_t (*httpc_headers_done_fn)(httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len, u32_t content_len);
  113. typedef struct _httpc_connection {
  114. ip_addr_t proxy_addr;
  115. u16_t proxy_port;
  116. u8_t use_proxy;
  117. /* @todo: add username:pass? */
  118. #if LWIP_ALTCP
  119. altcp_allocator_t *altcp_allocator;
  120. #endif
  121. /* this callback is called when the transfer is finished (or aborted) */
  122. httpc_result_fn result_fn;
  123. /* this callback is called after receiving the http headers
  124. It can abort the connection by returning != ERR_OK */
  125. httpc_headers_done_fn headers_done_fn;
  126. } httpc_connection_t;
  127. err_t httpc_get_file(const ip_addr_t* server_addr, u16_t port, const char* uri, const httpc_connection_t *settings,
  128. altcp_recv_fn recv_fn, void* callback_arg, httpc_state_t **connection);
  129. err_t httpc_get_file_dns(const char* server_name, u16_t port, const char* uri, const httpc_connection_t *settings,
  130. altcp_recv_fn recv_fn, void* callback_arg, httpc_state_t **connection);
  131. #if LWIP_HTTPC_HAVE_FILE_IO
  132. err_t httpc_get_file_to_disk(const ip_addr_t* server_addr, u16_t port, const char* uri, const httpc_connection_t *settings,
  133. void* callback_arg, const char* local_file_name, httpc_state_t **connection);
  134. err_t httpc_get_file_dns_to_disk(const char* server_name, u16_t port, const char* uri, const httpc_connection_t *settings,
  135. void* callback_arg, const char* local_file_name, httpc_state_t **connection);
  136. #endif /* LWIP_HTTPC_HAVE_FILE_IO */
  137. #ifdef __cplusplus
  138. }
  139. #endif
  140. #endif /* LWIP_TCP && LWIP_CALLBACK_API */
  141. #endif /* LWIP_HDR_APPS_HTTP_CLIENT_H */