f
Fork of lwip by
trunk/HTTPClient/HTTPClient.cpp@0:5e1631496985, 2012-05-08 (annotated)
- Committer:
- root@mbed.org
- Date:
- Tue May 08 15:32:10 2012 +0100
- Revision:
- 0:5e1631496985
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
root@mbed.org | 0:5e1631496985 | 1 | #include "HTTPClient.h" |
root@mbed.org | 0:5e1631496985 | 2 | #include "NetServer.h" |
root@mbed.org | 0:5e1631496985 | 3 | #include "iputil.h" |
root@mbed.org | 0:5e1631496985 | 4 | |
root@mbed.org | 0:5e1631496985 | 5 | using namespace mbed; |
root@mbed.org | 0:5e1631496985 | 6 | using namespace std; |
root@mbed.org | 0:5e1631496985 | 7 | |
root@mbed.org | 0:5e1631496985 | 8 | #define POST 0x1 |
root@mbed.org | 0:5e1631496985 | 9 | #define FDATA 0x2 |
root@mbed.org | 0:5e1631496985 | 10 | #define FRES 0x4 |
root@mbed.org | 0:5e1631496985 | 11 | #define GET 0x0 |
root@mbed.org | 0:5e1631496985 | 12 | #define CDATA 0x0 |
root@mbed.org | 0:5e1631496985 | 13 | #define CRES 0x0 |
root@mbed.org | 0:5e1631496985 | 14 | |
root@mbed.org | 0:5e1631496985 | 15 | long fleft(FILE *fd) { |
root@mbed.org | 0:5e1631496985 | 16 | long len, cur; |
root@mbed.org | 0:5e1631496985 | 17 | cur = ftell(fd); |
root@mbed.org | 0:5e1631496985 | 18 | fseek(fd, 0, SEEK_END); |
root@mbed.org | 0:5e1631496985 | 19 | len = ftell(fd); |
root@mbed.org | 0:5e1631496985 | 20 | fseek(fd, cur, SEEK_SET); |
root@mbed.org | 0:5e1631496985 | 21 | return len; |
root@mbed.org | 0:5e1631496985 | 22 | } |
root@mbed.org | 0:5e1631496985 | 23 | |
root@mbed.org | 0:5e1631496985 | 24 | |
root@mbed.org | 0:5e1631496985 | 25 | HTTPClient::HTTPClient(const char *hostname, struct ip_addr ip, struct ip_addr nm, struct ip_addr gw, struct ip_addr dns) |
root@mbed.org | 0:5e1631496985 | 26 | : TCPConnection(), _auth(NULL), _timeout(0), _data(NULL), _headerfields(NULL), _timeout_max(60000) { |
root@mbed.org | 0:5e1631496985 | 27 | NetServer *net = NULL; |
root@mbed.org | 0:5e1631496985 | 28 | if(ip.addr != ip_addr_any.addr && nm.addr != ip_addr_any.addr && gw.addr != ip_addr_any.addr) { |
root@mbed.org | 0:5e1631496985 | 29 | net = NetServer::create(ip, nm, gw); |
root@mbed.org | 0:5e1631496985 | 30 | if(dns.addr != ip_addr_any.addr) { |
root@mbed.org | 0:5e1631496985 | 31 | net->setDNS1(dns); |
root@mbed.org | 0:5e1631496985 | 32 | } |
root@mbed.org | 0:5e1631496985 | 33 | } else if(hostname) { |
root@mbed.org | 0:5e1631496985 | 34 | net = NetServer::create(); |
root@mbed.org | 0:5e1631496985 | 35 | } |
root@mbed.org | 0:5e1631496985 | 36 | if(hostname) { |
root@mbed.org | 0:5e1631496985 | 37 | net->setHostname(hostname); |
root@mbed.org | 0:5e1631496985 | 38 | } |
root@mbed.org | 0:5e1631496985 | 39 | } |
root@mbed.org | 0:5e1631496985 | 40 | |
root@mbed.org | 0:5e1631496985 | 41 | void HTTPClient::timeout(int value) { |
root@mbed.org | 0:5e1631496985 | 42 | _timeout_max = value; |
root@mbed.org | 0:5e1631496985 | 43 | } |
root@mbed.org | 0:5e1631496985 | 44 | |
root@mbed.org | 0:5e1631496985 | 45 | void HTTPClient::err(err_t err) { |
root@mbed.org | 0:5e1631496985 | 46 | release_callbacks(); |
root@mbed.org | 0:5e1631496985 | 47 | } |
root@mbed.org | 0:5e1631496985 | 48 | |
root@mbed.org | 0:5e1631496985 | 49 | err_t HTTPClient::poll() { |
root@mbed.org | 0:5e1631496985 | 50 | if(NetServer::time() - _timeout > _timeout_max) { |
root@mbed.org | 0:5e1631496985 | 51 | release_callbacks(); |
root@mbed.org | 0:5e1631496985 | 52 | close(); |
root@mbed.org | 0:5e1631496985 | 53 | printf("TIMEOUT\n"); |
root@mbed.org | 0:5e1631496985 | 54 | _ready = true; |
root@mbed.org | 0:5e1631496985 | 55 | _state = 99; |
root@mbed.org | 0:5e1631496985 | 56 | } |
root@mbed.org | 0:5e1631496985 | 57 | return ERR_OK; |
root@mbed.org | 0:5e1631496985 | 58 | } |
root@mbed.org | 0:5e1631496985 | 59 | |
root@mbed.org | 0:5e1631496985 | 60 | void HTTPClient::dnsreply(const char *hostname, struct ip_addr *ipaddr) { |
root@mbed.org | 0:5e1631496985 | 61 | _ready = true; |
root@mbed.org | 0:5e1631496985 | 62 | _ipaddr = *ipaddr; |
root@mbed.org | 0:5e1631496985 | 63 | _state = (ipaddr==NULL)? 99 : 0; |
root@mbed.org | 0:5e1631496985 | 64 | _timeout = NetServer::time(); |
root@mbed.org | 0:5e1631496985 | 65 | } |
root@mbed.org | 0:5e1631496985 | 66 | |
root@mbed.org | 0:5e1631496985 | 67 | err_t HTTPClient::connected(err_t err) { |
root@mbed.org | 0:5e1631496985 | 68 | TCPConnection::connected(err); |
root@mbed.org | 0:5e1631496985 | 69 | _ready = false; |
root@mbed.org | 0:5e1631496985 | 70 | _state = 0; |
root@mbed.org | 0:5e1631496985 | 71 | _resultoff = 0; |
root@mbed.org | 0:5e1631496985 | 72 | if(_mode&POST) { |
root@mbed.org | 0:5e1631496985 | 73 | write((void *)"POST ", 5, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); |
root@mbed.org | 0:5e1631496985 | 74 | } else { |
root@mbed.org | 0:5e1631496985 | 75 | write((void *)"GET ", 4, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); |
root@mbed.org | 0:5e1631496985 | 76 | } |
root@mbed.org | 0:5e1631496985 | 77 | if(strlen(_path) == 0) { |
root@mbed.org | 0:5e1631496985 | 78 | write((void *)"/", 1, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); |
root@mbed.org | 0:5e1631496985 | 79 | } else { |
root@mbed.org | 0:5e1631496985 | 80 | write((void *)_path, strlen(_path), TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); |
root@mbed.org | 0:5e1631496985 | 81 | } |
root@mbed.org | 0:5e1631496985 | 82 | write((void *)" HTTP/1.1\r\nHost: ", 17, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); |
root@mbed.org | 0:5e1631496985 | 83 | write((void *)_host, _hostlen, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); |
root@mbed.org | 0:5e1631496985 | 84 | if(_auth&&(*_auth!='\0')) { |
root@mbed.org | 0:5e1631496985 | 85 | write((void *)"\r\nAuthorization: Basic ", 23, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); |
root@mbed.org | 0:5e1631496985 | 86 | write((void *)_auth, strlen(_auth), TCP_WRITE_FLAG_COPY |TCP_WRITE_FLAG_MORE); |
root@mbed.org | 0:5e1631496985 | 87 | } |
root@mbed.org | 0:5e1631496985 | 88 | if(_headerfields&&(*_headerfields!='\0')) { |
root@mbed.org | 0:5e1631496985 | 89 | write((void *)"\r\n", 2, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); |
root@mbed.org | 0:5e1631496985 | 90 | write((void *)_headerfields, strlen(_headerfields), TCP_WRITE_FLAG_COPY |TCP_WRITE_FLAG_MORE); |
root@mbed.org | 0:5e1631496985 | 91 | } |
root@mbed.org | 0:5e1631496985 | 92 | write((void *)"\r\nConnection: Close\r\n", 21, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_COPY); |
root@mbed.org | 0:5e1631496985 | 93 | if(_data) { |
root@mbed.org | 0:5e1631496985 | 94 | char clen[256]; |
root@mbed.org | 0:5e1631496985 | 95 | int len, blen; |
root@mbed.org | 0:5e1631496985 | 96 | if(_mode&FDATA) { |
root@mbed.org | 0:5e1631496985 | 97 | //printf("Send file\n"); |
root@mbed.org | 0:5e1631496985 | 98 | len = fleft((FILE *)_data); |
root@mbed.org | 0:5e1631496985 | 99 | sprintf(clen, "Content-Length: %d\r\n\r\n\0", len); |
root@mbed.org | 0:5e1631496985 | 100 | write((void *)clen, strlen(clen), TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_COPY); |
root@mbed.org | 0:5e1631496985 | 101 | if(len) { |
root@mbed.org | 0:5e1631496985 | 102 | do { |
root@mbed.org | 0:5e1631496985 | 103 | int sb = sndbuf(); |
root@mbed.org | 0:5e1631496985 | 104 | blen = fread(clen, sizeof(char), (int)min(sb, 100), (FILE *)_data); |
root@mbed.org | 0:5e1631496985 | 105 | write(clen, blen, (TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE)); |
root@mbed.org | 0:5e1631496985 | 106 | len -= blen; |
root@mbed.org | 0:5e1631496985 | 107 | } while(len > 1 && blen); |
root@mbed.org | 0:5e1631496985 | 108 | } |
root@mbed.org | 0:5e1631496985 | 109 | } else { |
root@mbed.org | 0:5e1631496985 | 110 | len = strlen((const char *)_data); |
root@mbed.org | 0:5e1631496985 | 111 | sprintf(clen, "Content-Length: %d\r\n\r\n\0", len); |
root@mbed.org | 0:5e1631496985 | 112 | write((void *)clen, strlen(clen), TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_COPY); |
root@mbed.org | 0:5e1631496985 | 113 | write((void *)_data, len, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); |
root@mbed.org | 0:5e1631496985 | 114 | } |
root@mbed.org | 0:5e1631496985 | 115 | } |
root@mbed.org | 0:5e1631496985 | 116 | write((void *)"\r\n", 2, TCP_WRITE_FLAG_COPY); |
root@mbed.org | 0:5e1631496985 | 117 | _timeout = NetServer::time(); |
root@mbed.org | 0:5e1631496985 | 118 | return ERR_OK; |
root@mbed.org | 0:5e1631496985 | 119 | } |
root@mbed.org | 0:5e1631496985 | 120 | |
root@mbed.org | 0:5e1631496985 | 121 | #define _min(x, y) (((x)<(y))?(x):(y)) |
root@mbed.org | 0:5e1631496985 | 122 | err_t HTTPClient::recv(struct pbuf *p, err_t err) { |
root@mbed.org | 0:5e1631496985 | 123 | if(err == ERR_OK && p != NULL&&_state<10) { |
root@mbed.org | 0:5e1631496985 | 124 | _timeout = NetServer::time(); |
root@mbed.org | 0:5e1631496985 | 125 | struct pbuf *q = p; |
root@mbed.org | 0:5e1631496985 | 126 | char *d = (char *)q->payload; |
root@mbed.org | 0:5e1631496985 | 127 | recved(p->tot_len); |
root@mbed.org | 0:5e1631496985 | 128 | |
root@mbed.org | 0:5e1631496985 | 129 | while(q&&_state<10) { |
root@mbed.org | 0:5e1631496985 | 130 | unsigned int end = ((unsigned int)(q->payload)+(unsigned int)(q->len)); |
root@mbed.org | 0:5e1631496985 | 131 | switch(_state) { |
root@mbed.org | 0:5e1631496985 | 132 | case 0: { |
root@mbed.org | 0:5e1631496985 | 133 | for(; _state==0 && ((unsigned int)d < end); d++) { |
root@mbed.org | 0:5e1631496985 | 134 | if((*((char *)(d-0))=='\n')&&(*((char *)(d-1))=='\r')&& |
root@mbed.org | 0:5e1631496985 | 135 | (*((char *)(d-2))=='\n')&&(*((char *)(d-3))=='\r')) { |
root@mbed.org | 0:5e1631496985 | 136 | _state = 1; |
root@mbed.org | 0:5e1631496985 | 137 | d += 1; |
root@mbed.org | 0:5e1631496985 | 138 | break; |
root@mbed.org | 0:5e1631496985 | 139 | } |
root@mbed.org | 0:5e1631496985 | 140 | } |
root@mbed.org | 0:5e1631496985 | 141 | } break; |
root@mbed.org | 0:5e1631496985 | 142 | case 1: { |
root@mbed.org | 0:5e1631496985 | 143 | if(_result) { |
root@mbed.org | 0:5e1631496985 | 144 | if(_mode&FRES) { |
root@mbed.org | 0:5e1631496985 | 145 | fwrite(d, sizeof(char), end - (unsigned int)d, (FILE *)_result); |
root@mbed.org | 0:5e1631496985 | 146 | _resultoff += (end - (unsigned int)d); |
root@mbed.org | 0:5e1631496985 | 147 | d = (char *)end; |
root@mbed.org | 0:5e1631496985 | 148 | } else { |
root@mbed.org | 0:5e1631496985 | 149 | unsigned int len = _min(_resultleft, (end-(unsigned int)d)); |
root@mbed.org | 0:5e1631496985 | 150 | memcpy((char *)_result + _resultoff, d, len); |
root@mbed.org | 0:5e1631496985 | 151 | _resultleft -= len; |
root@mbed.org | 0:5e1631496985 | 152 | _resultoff += len; |
root@mbed.org | 0:5e1631496985 | 153 | d += len; |
root@mbed.org | 0:5e1631496985 | 154 | |
root@mbed.org | 0:5e1631496985 | 155 | if(!_resultleft) { |
root@mbed.org | 0:5e1631496985 | 156 | _state = 10; |
root@mbed.org | 0:5e1631496985 | 157 | } |
root@mbed.org | 0:5e1631496985 | 158 | } |
root@mbed.org | 0:5e1631496985 | 159 | } else { |
root@mbed.org | 0:5e1631496985 | 160 | _state = 10; |
root@mbed.org | 0:5e1631496985 | 161 | } |
root@mbed.org | 0:5e1631496985 | 162 | } break; |
root@mbed.org | 0:5e1631496985 | 163 | default: { |
root@mbed.org | 0:5e1631496985 | 164 | break; |
root@mbed.org | 0:5e1631496985 | 165 | } |
root@mbed.org | 0:5e1631496985 | 166 | } |
root@mbed.org | 0:5e1631496985 | 167 | if(_state<10&&(unsigned int)d==end) { |
root@mbed.org | 0:5e1631496985 | 168 | q = q->next; |
root@mbed.org | 0:5e1631496985 | 169 | if(q) { |
root@mbed.org | 0:5e1631496985 | 170 | d = static_cast<char *>(q->payload); |
root@mbed.org | 0:5e1631496985 | 171 | } |
root@mbed.org | 0:5e1631496985 | 172 | } |
root@mbed.org | 0:5e1631496985 | 173 | } |
root@mbed.org | 0:5e1631496985 | 174 | } |
root@mbed.org | 0:5e1631496985 | 175 | |
root@mbed.org | 0:5e1631496985 | 176 | if(p!=NULL) { |
root@mbed.org | 0:5e1631496985 | 177 | pbuf_free(p); |
root@mbed.org | 0:5e1631496985 | 178 | } |
root@mbed.org | 0:5e1631496985 | 179 | |
root@mbed.org | 0:5e1631496985 | 180 | if(_state>10||p==NULL||err!=ERR_OK) { |
root@mbed.org | 0:5e1631496985 | 181 | release_callbacks(); |
root@mbed.org | 0:5e1631496985 | 182 | close(); |
root@mbed.org | 0:5e1631496985 | 183 | _ready = true; |
root@mbed.org | 0:5e1631496985 | 184 | } |
root@mbed.org | 0:5e1631496985 | 185 | return ERR_OK; |
root@mbed.org | 0:5e1631496985 | 186 | } |
root@mbed.org | 0:5e1631496985 | 187 | |
root@mbed.org | 0:5e1631496985 | 188 | unsigned int HTTPClient::make(const char *request) { |
root@mbed.org | 0:5e1631496985 | 189 | _request = request; |
root@mbed.org | 0:5e1631496985 | 190 | _resultoff = 0; |
root@mbed.org | 0:5e1631496985 | 191 | _ready = false; |
root@mbed.org | 0:5e1631496985 | 192 | _state = 0; |
root@mbed.org | 0:5e1631496985 | 193 | _hostlen = 0; |
root@mbed.org | 0:5e1631496985 | 194 | _port = 0; |
root@mbed.org | 0:5e1631496985 | 195 | _timeout = NetServer::time(); |
root@mbed.org | 0:5e1631496985 | 196 | NetServer::ready(); |
root@mbed.org | 0:5e1631496985 | 197 | |
root@mbed.org | 0:5e1631496985 | 198 | int hostlen = 0; |
root@mbed.org | 0:5e1631496985 | 199 | if(strlen(_request)<10||strncmp("http://", _request, 7)!=0) { |
root@mbed.org | 0:5e1631496985 | 200 | printf("Only http requests are allowed\n"); |
root@mbed.org | 0:5e1631496985 | 201 | return 0; |
root@mbed.org | 0:5e1631496985 | 202 | } |
root@mbed.org | 0:5e1631496985 | 203 | _path = _host = _request + 6; |
root@mbed.org | 0:5e1631496985 | 204 | _host++; |
root@mbed.org | 0:5e1631496985 | 205 | while(!_state == 1) { |
root@mbed.org | 0:5e1631496985 | 206 | switch(*(++_path)) { |
root@mbed.org | 0:5e1631496985 | 207 | case ':': |
root@mbed.org | 0:5e1631496985 | 208 | _port = atoi(_path+1); |
root@mbed.org | 0:5e1631496985 | 209 | break; |
root@mbed.org | 0:5e1631496985 | 210 | case '/': |
root@mbed.org | 0:5e1631496985 | 211 | case '\0': |
root@mbed.org | 0:5e1631496985 | 212 | _port = (_port)?_port:80; |
root@mbed.org | 0:5e1631496985 | 213 | _state = 1; |
root@mbed.org | 0:5e1631496985 | 214 | break; |
root@mbed.org | 0:5e1631496985 | 215 | default: |
root@mbed.org | 0:5e1631496985 | 216 | break; |
root@mbed.org | 0:5e1631496985 | 217 | } |
root@mbed.org | 0:5e1631496985 | 218 | if(!_port) { |
root@mbed.org | 0:5e1631496985 | 219 | hostlen++; |
root@mbed.org | 0:5e1631496985 | 220 | } |
root@mbed.org | 0:5e1631496985 | 221 | if(!_state == 1) { |
root@mbed.org | 0:5e1631496985 | 222 | _hostlen++; |
root@mbed.org | 0:5e1631496985 | 223 | } |
root@mbed.org | 0:5e1631496985 | 224 | } |
root@mbed.org | 0:5e1631496985 | 225 | _state = 0; |
root@mbed.org | 0:5e1631496985 | 226 | |
root@mbed.org | 0:5e1631496985 | 227 | if(hostlen>256) { |
root@mbed.org | 0:5e1631496985 | 228 | printf("Hostname longer than allowed\n"); |
root@mbed.org | 0:5e1631496985 | 229 | return 0; |
root@mbed.org | 0:5e1631496985 | 230 | } |
root@mbed.org | 0:5e1631496985 | 231 | |
root@mbed.org | 0:5e1631496985 | 232 | char host[257]; |
root@mbed.org | 0:5e1631496985 | 233 | memcpy(host, _host, hostlen); |
root@mbed.org | 0:5e1631496985 | 234 | host[hostlen] = 0; |
root@mbed.org | 0:5e1631496985 | 235 | _timeout = NetServer::time(); |
root@mbed.org | 0:5e1631496985 | 236 | struct in_addr in; |
root@mbed.org | 0:5e1631496985 | 237 | if(!inet_aton(host, &in)) { |
root@mbed.org | 0:5e1631496985 | 238 | _ready = false; |
root@mbed.org | 0:5e1631496985 | 239 | if(dnsrequest(host, &_ipaddr)==ERR_INPROGRESS) { |
root@mbed.org | 0:5e1631496985 | 240 | while(!_ready) { |
root@mbed.org | 0:5e1631496985 | 241 | NetServer::poll(); |
root@mbed.org | 0:5e1631496985 | 242 | wait_ms(10); |
root@mbed.org | 0:5e1631496985 | 243 | } |
root@mbed.org | 0:5e1631496985 | 244 | if(_state==99) { |
root@mbed.org | 0:5e1631496985 | 245 | printf("Server not found\n"); |
root@mbed.org | 0:5e1631496985 | 246 | return 0; |
root@mbed.org | 0:5e1631496985 | 247 | } |
root@mbed.org | 0:5e1631496985 | 248 | } |
root@mbed.org | 0:5e1631496985 | 249 | } else { |
root@mbed.org | 0:5e1631496985 | 250 | _ipaddr.addr = in.s_addr; |
root@mbed.org | 0:5e1631496985 | 251 | } |
root@mbed.org | 0:5e1631496985 | 252 | |
root@mbed.org | 0:5e1631496985 | 253 | _ready = false; |
root@mbed.org | 0:5e1631496985 | 254 | _timeout = NetServer::time(); |
root@mbed.org | 0:5e1631496985 | 255 | connect(); |
root@mbed.org | 0:5e1631496985 | 256 | set_poll_interval(10); |
root@mbed.org | 0:5e1631496985 | 257 | tcp_setprio(_pcb, TCP_PRIO_MIN); |
root@mbed.org | 0:5e1631496985 | 258 | while(!_ready) { |
root@mbed.org | 0:5e1631496985 | 259 | NetServer::poll(); |
root@mbed.org | 0:5e1631496985 | 260 | wait_ms(10); |
root@mbed.org | 0:5e1631496985 | 261 | } |
root@mbed.org | 0:5e1631496985 | 262 | //release_callbacks(); |
root@mbed.org | 0:5e1631496985 | 263 | close(); |
root@mbed.org | 0:5e1631496985 | 264 | if(_state==99) { |
root@mbed.org | 0:5e1631496985 | 265 | printf("Connection error\n"); |
root@mbed.org | 0:5e1631496985 | 266 | return 0; |
root@mbed.org | 0:5e1631496985 | 267 | } |
root@mbed.org | 0:5e1631496985 | 268 | |
root@mbed.org | 0:5e1631496985 | 269 | if(!_mode&FRES&&_result) { |
root@mbed.org | 0:5e1631496985 | 270 | ((char *)_result)[_resultoff+1] = '\0'; |
root@mbed.org | 0:5e1631496985 | 271 | } |
root@mbed.org | 0:5e1631496985 | 272 | return ((!_mode&FRES)&&!_result)?1:_resultoff; |
root@mbed.org | 0:5e1631496985 | 273 | |
root@mbed.org | 0:5e1631496985 | 274 | } |
root@mbed.org | 0:5e1631496985 | 275 | |
root@mbed.org | 0:5e1631496985 | 276 | void HTTPClient::auth(const char *user, const char *password) { |
root@mbed.org | 0:5e1631496985 | 277 | if(user) { |
root@mbed.org | 0:5e1631496985 | 278 | char up[256]; |
root@mbed.org | 0:5e1631496985 | 279 | sprintf(up, "%s:%s", user, password); |
root@mbed.org | 0:5e1631496985 | 280 | _auth = new char[base64enc_len(up)+1]; |
root@mbed.org | 0:5e1631496985 | 281 | base64enc(up, strlen(up), _auth); |
root@mbed.org | 0:5e1631496985 | 282 | } else if(_auth) { |
root@mbed.org | 0:5e1631496985 | 283 | delete _auth; |
root@mbed.org | 0:5e1631496985 | 284 | _auth = NULL; |
root@mbed.org | 0:5e1631496985 | 285 | } |
root@mbed.org | 0:5e1631496985 | 286 | } |
root@mbed.org | 0:5e1631496985 | 287 | |
root@mbed.org | 0:5e1631496985 | 288 | void HTTPClient::headers(const char *fields) { |
root@mbed.org | 0:5e1631496985 | 289 | _headerfields = fields; |
root@mbed.org | 0:5e1631496985 | 290 | } |
root@mbed.org | 0:5e1631496985 | 291 | |
root@mbed.org | 0:5e1631496985 | 292 | unsigned int HTTPClient::get(const char *url, char *result, int rsize) { |
root@mbed.org | 0:5e1631496985 | 293 | _mode = GET | CDATA | CRES; |
root@mbed.org | 0:5e1631496985 | 294 | _data = (void *)NULL; |
root@mbed.org | 0:5e1631496985 | 295 | _result = (void *)result; |
root@mbed.org | 0:5e1631496985 | 296 | _resultleft = rsize -1; |
root@mbed.org | 0:5e1631496985 | 297 | |
root@mbed.org | 0:5e1631496985 | 298 | return make(url); |
root@mbed.org | 0:5e1631496985 | 299 | } |
root@mbed.org | 0:5e1631496985 | 300 | |
root@mbed.org | 0:5e1631496985 | 301 | unsigned int HTTPClient::get(const char *url, FILE *result) { |
root@mbed.org | 0:5e1631496985 | 302 | _mode = GET | CDATA | FRES; |
root@mbed.org | 0:5e1631496985 | 303 | _data = (void *)NULL; |
root@mbed.org | 0:5e1631496985 | 304 | _result = (void *)result; |
root@mbed.org | 0:5e1631496985 | 305 | _resultleft = 0; |
root@mbed.org | 0:5e1631496985 | 306 | |
root@mbed.org | 0:5e1631496985 | 307 | return make(url); |
root@mbed.org | 0:5e1631496985 | 308 | } |
root@mbed.org | 0:5e1631496985 | 309 | |
root@mbed.org | 0:5e1631496985 | 310 | unsigned int HTTPClient::post(const char *url, const char *data, char *result, int rsize) { |
root@mbed.org | 0:5e1631496985 | 311 | _mode = POST | CDATA | CRES; |
root@mbed.org | 0:5e1631496985 | 312 | _data = (void *)data; |
root@mbed.org | 0:5e1631496985 | 313 | _result = (void *)result; |
root@mbed.org | 0:5e1631496985 | 314 | _resultleft = rsize -1; |
root@mbed.org | 0:5e1631496985 | 315 | |
root@mbed.org | 0:5e1631496985 | 316 | return make(url); |
root@mbed.org | 0:5e1631496985 | 317 | } |
root@mbed.org | 0:5e1631496985 | 318 | |
root@mbed.org | 0:5e1631496985 | 319 | unsigned int HTTPClient::post(const char *url, const char *data, FILE *result) { |
root@mbed.org | 0:5e1631496985 | 320 | _mode = POST | CDATA | FRES; |
root@mbed.org | 0:5e1631496985 | 321 | _data = (void *)data; |
root@mbed.org | 0:5e1631496985 | 322 | _result = (void *)result; |
root@mbed.org | 0:5e1631496985 | 323 | _resultleft = 0; |
root@mbed.org | 0:5e1631496985 | 324 | |
root@mbed.org | 0:5e1631496985 | 325 | return make(url); |
root@mbed.org | 0:5e1631496985 | 326 | } |
root@mbed.org | 0:5e1631496985 | 327 | |
root@mbed.org | 0:5e1631496985 | 328 | unsigned int HTTPClient::post(const char *url, FILE *data, FILE *result) { |
root@mbed.org | 0:5e1631496985 | 329 | _mode = POST | FDATA | FRES; |
root@mbed.org | 0:5e1631496985 | 330 | _data = (void *)data; |
root@mbed.org | 0:5e1631496985 | 331 | _result = (void *)result; |
root@mbed.org | 0:5e1631496985 | 332 | _resultleft = 0; |
root@mbed.org | 0:5e1631496985 | 333 | |
root@mbed.org | 0:5e1631496985 | 334 | return make(url); |
root@mbed.org | 0:5e1631496985 | 335 | } |
root@mbed.org | 0:5e1631496985 | 336 | |
root@mbed.org | 0:5e1631496985 | 337 | unsigned int HTTPClient::post(const char *url, FILE *data, char *result, int rsize) { |
root@mbed.org | 0:5e1631496985 | 338 | _mode = POST | FDATA | CRES; |
root@mbed.org | 0:5e1631496985 | 339 | _data = (void *)data; |
root@mbed.org | 0:5e1631496985 | 340 | _result = (void *)result; |
root@mbed.org | 0:5e1631496985 | 341 | _resultleft = rsize -1; |
root@mbed.org | 0:5e1631496985 | 342 | |
root@mbed.org | 0:5e1631496985 | 343 | return make(url); |
root@mbed.org | 0:5e1631496985 | 344 | } |
root@mbed.org | 0:5e1631496985 | 345 |