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