This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088
Fork of libMiMic by
mbed/HttpClient.cpp@115:fa79286d8ea4, 2017-02-24 (annotated)
- Committer:
- furutani
- Date:
- Fri Feb 24 04:43:41 2017 +0000
- Revision:
- 115:fa79286d8ea4
- Parent:
- 62:8be8c5924c3e
Delete missing include line.; Add parameter "timeout" to TCPSocket::connect(), precv().; Fix to send ARP request to default gateway when connecting to IP address of different segment.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nyatla | 58:03b89038b21a | 1 | #include "HttpClient.h" |
nyatla | 58:03b89038b21a | 2 | |
nyatla | 58:03b89038b21a | 3 | namespace MiMic |
nyatla | 58:03b89038b21a | 4 | { |
nyatla | 58:03b89038b21a | 5 | HttpClient::HttpClient() |
nyatla | 58:03b89038b21a | 6 | { |
nyatla | 58:03b89038b21a | 7 | this->_private_tcp_rx_buf=malloc(256); |
nyatla | 58:03b89038b21a | 8 | NyLPC_cHttpClient_initialize(&this->_inst,this->_private_tcp_rx_buf,256); |
nyatla | 58:03b89038b21a | 9 | } |
nyatla | 58:03b89038b21a | 10 | HttpClient::~HttpClient() |
nyatla | 58:03b89038b21a | 11 | { |
nyatla | 58:03b89038b21a | 12 | NyLPC_cHttpClient_finalize(&this->_inst); |
nyatla | 58:03b89038b21a | 13 | if(this->_private_tcp_rx_buf!=NULL){ |
nyatla | 58:03b89038b21a | 14 | free(this->_private_tcp_rx_buf); |
nyatla | 58:03b89038b21a | 15 | } |
nyatla | 58:03b89038b21a | 16 | } |
nyatla | 58:03b89038b21a | 17 | bool HttpClient::connect(const IpAddr& i_host,unsigned short i_port) |
nyatla | 58:03b89038b21a | 18 | { |
nyatla | 58:03b89038b21a | 19 | return NyLPC_cHttpClient_connect(&this->_inst,&i_host.addr.v4,i_port)?true:false; |
nyatla | 58:03b89038b21a | 20 | } |
nyatla | 58:03b89038b21a | 21 | /** |
nyatla | 58:03b89038b21a | 22 | * This function sends a request to server and prevent to accept status code. |
nyatla | 58:03b89038b21a | 23 | * Must call getStatus after successful. |
nyatla | 58:03b89038b21a | 24 | * If request has content body(i_content_length!=0), call writeX function to send request body in before to call getStatus |
nyatla | 58:03b89038b21a | 25 | * @return |
nyatla | 58:03b89038b21a | 26 | * true if successful, |
nyatla | 58:03b89038b21a | 27 | * otherwise error. Connection is closed. |
nyatla | 58:03b89038b21a | 28 | * @example |
nyatla | 58:03b89038b21a | 29 | * <code> |
nyatla | 58:03b89038b21a | 30 | * //GET |
nyatla | 58:03b89038b21a | 31 | * </code> |
nyatla | 58:03b89038b21a | 32 | * <code> |
nyatla | 58:03b89038b21a | 33 | * //POST |
nyatla | 58:03b89038b21a | 34 | * |
nyatla | 58:03b89038b21a | 35 | * </code> |
nyatla | 58:03b89038b21a | 36 | */ |
nyatla | 58:03b89038b21a | 37 | bool HttpClient::sendMethod(NyLPC_THttpMethodType i_method,const char* i_path,int i_content_length,const char* i_mimetype,const char* i_additional_header) |
nyatla | 58:03b89038b21a | 38 | { |
nyatla | 58:03b89038b21a | 39 | return NyLPC_cHttpClient_sendMethod(&this->_inst,i_method,i_path,i_content_length,i_mimetype,i_additional_header)?true:false; |
nyatla | 58:03b89038b21a | 40 | } |
nyatla | 58:03b89038b21a | 41 | /** |
nyatla | 58:03b89038b21a | 42 | * This function returns status code. |
nyatla | 58:03b89038b21a | 43 | * Must call after the sendMethod was successful. |
nyatla | 58:03b89038b21a | 44 | * @return |
nyatla | 58:03b89038b21a | 45 | * Error:0,otherwise HTTP status code. |
nyatla | 58:03b89038b21a | 46 | */ |
nyatla | 58:03b89038b21a | 47 | int HttpClient::getStatus() |
nyatla | 58:03b89038b21a | 48 | { |
nyatla | 58:03b89038b21a | 49 | return NyLPC_cHttpClient_getStatus(&this->_inst); |
nyatla | 58:03b89038b21a | 50 | } |
nyatla | 58:03b89038b21a | 51 | /** |
nyatla | 58:03b89038b21a | 52 | * Close current connection. |
nyatla | 58:03b89038b21a | 53 | */ |
nyatla | 58:03b89038b21a | 54 | void HttpClient::close() |
nyatla | 58:03b89038b21a | 55 | { |
nyatla | 58:03b89038b21a | 56 | NyLPC_cHttpClient_close(&this->_inst); |
nyatla | 58:03b89038b21a | 57 | } |
nyatla | 58:03b89038b21a | 58 | /** |
nyatla | 58:03b89038b21a | 59 | * Read request body from http stream. |
nyatla | 58:03b89038b21a | 60 | * @param i_rx_buf |
nyatla | 58:03b89038b21a | 61 | * A buffer which accepts received data. |
nyatla | 58:03b89038b21a | 62 | * @param i_rx_buf_len |
nyatla | 58:03b89038b21a | 63 | * size of i_rx_buf in byte. |
nyatla | 58:03b89038b21a | 64 | * @param i_read_len |
nyatla | 58:03b89038b21a | 65 | * pointer to variable which accept received data size in byte. |
nyatla | 58:03b89038b21a | 66 | * It is enabled in retrurn true. |
nyatla | 58:03b89038b21a | 67 | * n>0 is datasize. n==0 is end of stream. |
nyatla | 58:03b89038b21a | 68 | * @return |
nyatla | 58:03b89038b21a | 69 | * true if successful,otherwise false |
nyatla | 58:03b89038b21a | 70 | */ |
nyatla | 62:8be8c5924c3e | 71 | bool HttpClient::read(void* i_rx_buf,int i_rx_buf_len,short &i_read_len) |
nyatla | 58:03b89038b21a | 72 | { |
nyatla | 58:03b89038b21a | 73 | return NyLPC_cHttpClient_read(&this->_inst,i_rx_buf,i_rx_buf_len,&i_read_len)?true:false; |
nyatla | 58:03b89038b21a | 74 | } |
nyatla | 58:03b89038b21a | 75 | bool HttpClient::write(const void* i_tx_buf,int i_tx_len) |
nyatla | 58:03b89038b21a | 76 | { |
nyatla | 58:03b89038b21a | 77 | return NyLPC_cHttpClient_write(&this->_inst,i_tx_buf,i_tx_len)?true:false; |
nyatla | 58:03b89038b21a | 78 | } |
nyatla | 58:03b89038b21a | 79 | |
nyatla | 58:03b89038b21a | 80 | } |