Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers http_client.h Source File

http_client.h

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * HTTP client
00004  */
00005 
00006 /*
00007  * Copyright (c) 2018 Simon Goldschmidt <goldsimon@gmx.de>
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without modification,
00011  * are permitted provided that the following conditions are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright notice,
00014  *    this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright notice,
00016  *    this list of conditions and the following disclaimer in the documentation
00017  *    and/or other materials provided with the distribution.
00018  * 3. The name of the author may not be used to endorse or promote products
00019  *    derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00023  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00024  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00026  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00029  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00030  * OF SUCH DAMAGE.
00031  *
00032  * This file is part of the lwIP TCP/IP stack.
00033  *
00034  * Author: Simon Goldschmidt <goldsimon@gmx.de>
00035  *
00036  */
00037 
00038 #ifndef LWIP_HDR_APPS_HTTP_CLIENT_H
00039 #define LWIP_HDR_APPS_HTTP_CLIENT_H
00040 
00041 #include "lwip/opt.h"
00042 #include "lwip/ip_addr.h"
00043 #include "lwip/err.h"
00044 #include "lwip/altcp.h"
00045 #include "lwip/prot/iana.h"
00046 #include "lwip/pbuf.h"
00047 
00048 #if LWIP_TCP && LWIP_CALLBACK_API
00049 
00050 #ifdef __cplusplus
00051 extern "C" {
00052 #endif
00053 
00054 /**
00055  * @ingroup httpc 
00056  * HTTPC_HAVE_FILE_IO: define this to 1 to have functions dowloading directly
00057  * to disk via fopen/fwrite.
00058  * These functions are example implementations of the interface only.
00059  */
00060 #ifndef LWIP_HTTPC_HAVE_FILE_IO
00061 #define LWIP_HTTPC_HAVE_FILE_IO   0
00062 #endif
00063 
00064 /**
00065  * @ingroup httpc 
00066  * The default TCP port used for HTTP
00067  */
00068 #define HTTP_DEFAULT_PORT         LWIP_IANA_PORT_HTTP
00069 
00070 /**
00071  * @ingroup httpc 
00072  * HTTP client result codes
00073  */
00074 typedef enum ehttpc_result {
00075   /** File successfully received */
00076   HTTPC_RESULT_OK            = 0,
00077   /** Unknown error */
00078   HTTPC_RESULT_ERR_UNKNOWN   = 1,
00079   /** Connection to server failed */
00080   HTTPC_RESULT_ERR_CONNECT   = 2,
00081   /** Failed to resolve server hostname */
00082   HTTPC_RESULT_ERR_HOSTNAME  = 3,
00083   /** Connection unexpectedly closed by remote server */
00084   HTTPC_RESULT_ERR_CLOSED    = 4,
00085   /** Connection timed out (server didn't respond in time) */
00086   HTTPC_RESULT_ERR_TIMEOUT   = 5,
00087   /** Server responded with an error code */
00088   HTTPC_RESULT_ERR_SVR_RESP  = 6,
00089   /** Local memory error */
00090   HTTPC_RESULT_ERR_MEM       = 7,
00091   /** Local abort */
00092   HTTPC_RESULT_LOCAL_ABORT   = 8,
00093   /** Content length mismatch */
00094   HTTPC_RESULT_ERR_CONTENT_LEN = 9
00095 } httpc_result_t;
00096 
00097 typedef struct _httpc_state httpc_state_t;
00098 
00099 /**
00100  * @ingroup httpc 
00101  * Prototype of a http client callback function
00102  *
00103  * @param arg argument specified when initiating the request
00104  * @param httpc_result result of the http transfer (see enum httpc_result_t)
00105  * @param rx_content_len number of bytes received (without headers)
00106  * @param srv_res this contains the http status code received (if any)
00107  * @param err an error returned by internal lwip functions, can help to specify
00108  *            the source of the error but must not necessarily be != ERR_OK
00109  */
00110 typedef void (*httpc_result_fn)(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, err_t err);
00111 
00112 /**
00113  * @ingroup httpc 
00114  * Prototype of http client callback: called when the headers are received
00115  *
00116  * @param connection http client connection
00117  * @param arg argument specified when initiating the request
00118  * @param hdr header pbuf(s) (may contain data also)
00119  * @param hdr_len length of the heders in 'hdr'
00120  * @param content_len content length as received in the headers (-1 if not received)
00121  * @return if != ERR_OK is returned, the connection is aborted
00122  */
00123 typedef err_t (*httpc_headers_done_fn)(httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len, u32_t content_len);
00124 
00125 typedef struct _httpc_connection {
00126   ip_addr_t proxy_addr;
00127   u16_t proxy_port;
00128   u8_t use_proxy;
00129   /* @todo: add username:pass? */
00130 
00131 #if LWIP_ALTCP
00132   altcp_allocator_t *altcp_allocator;
00133 #endif
00134 
00135   /* this callback is called when the transfer is finished (or aborted) */
00136   httpc_result_fn result_fn;
00137   /* this callback is called after receiving the http headers
00138      It can abort the connection by returning != ERR_OK */
00139   httpc_headers_done_fn headers_done_fn;
00140 } httpc_connection_t;
00141 
00142 err_t httpc_get_file(const ip_addr_t* server_addr, u16_t port, const char* uri, const httpc_connection_t *settings,
00143                      altcp_recv_fn recv_fn, void* callback_arg, httpc_state_t **connection);
00144 err_t httpc_get_file_dns(const char* server_name, u16_t port, const char* uri, const httpc_connection_t *settings,
00145                      altcp_recv_fn recv_fn, void* callback_arg, httpc_state_t **connection);
00146 
00147 #if LWIP_HTTPC_HAVE_FILE_IO
00148 err_t httpc_get_file_to_disk(const ip_addr_t* server_addr, u16_t port, const char* uri, const httpc_connection_t *settings,
00149                      void* callback_arg, const char* local_file_name, httpc_state_t **connection);
00150 err_t httpc_get_file_dns_to_disk(const char* server_name, u16_t port, const char* uri, const httpc_connection_t *settings,
00151                      void* callback_arg, const char* local_file_name, httpc_state_t **connection);
00152 #endif /* LWIP_HTTPC_HAVE_FILE_IO */
00153 
00154 #ifdef __cplusplus
00155 }
00156 #endif
00157 
00158 #endif /* LWIP_TCP && LWIP_CALLBACK_API */
00159 
00160 #endif /* LWIP_HDR_APPS_HTTP_CLIENT_H */