For PETEY

Dependencies:   mbed EthernetNetIf

Committer:
okini3939
Date:
Thu Jul 28 16:35:09 2011 +0000
Revision:
2:764ecec3dc59
Parent:
1:9f15e579d914

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:f2bf5f966801 1 /*
okini3939 0:f2bf5f966801 2 * mbed Tiny HTTP Client
okini3939 0:f2bf5f966801 3 * Copyright (c) 2011 Hiroshi Suga
okini3939 0:f2bf5f966801 4 * Released under the MIT License: http://mbed.org/license/mit
okini3939 0:f2bf5f966801 5 */
okini3939 0:f2bf5f966801 6
okini3939 0:f2bf5f966801 7 /** @file
okini3939 0:f2bf5f966801 8 * @brief Tiny HTTP Client
okini3939 0:f2bf5f966801 9 */
okini3939 0:f2bf5f966801 10
okini3939 0:f2bf5f966801 11 #include "mbed.h"
okini3939 0:f2bf5f966801 12 #include "EthernetNetIf.h"
okini3939 0:f2bf5f966801 13 #include "TCPSocket.h"
okini3939 0:f2bf5f966801 14 #include "DNSRequest.h"
okini3939 0:f2bf5f966801 15 #include "TinyHTTP.h"
okini3939 2:764ecec3dc59 16 #include <ctype.h>
okini3939 0:f2bf5f966801 17
okini3939 0:f2bf5f966801 18
okini3939 0:f2bf5f966801 19 TCPSocket *http;
okini3939 2:764ecec3dc59 20 volatile int tcp_ready, tcp_readable, tcp_writable;
okini3939 2:764ecec3dc59 21 volatile int dns_status;
okini3939 0:f2bf5f966801 22
okini3939 0:f2bf5f966801 23 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
okini3939 2:764ecec3dc59 24 int base64enc(const char *input, unsigned int length, char *output, int len) {
okini3939 0:f2bf5f966801 25 static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
okini3939 0:f2bf5f966801 26 unsigned int c, c1, c2, c3;
okini3939 2:764ecec3dc59 27
okini3939 2:764ecec3dc59 28 if (len < ((((length-1)/3)+1)<<2)) return -1;
okini3939 0:f2bf5f966801 29 for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) {
okini3939 0:f2bf5f966801 30 c1 = ((((unsigned char)*((unsigned char *)&input[i]))));
okini3939 0:f2bf5f966801 31 c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0;
okini3939 0:f2bf5f966801 32 c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0;
okini3939 0:f2bf5f966801 33
okini3939 0:f2bf5f966801 34 c = ((c1 & 0xFC) >> 2);
okini3939 0:f2bf5f966801 35 output[j+0] = base64[c];
okini3939 0:f2bf5f966801 36 c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4);
okini3939 0:f2bf5f966801 37 output[j+1] = base64[c];
okini3939 0:f2bf5f966801 38 c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6);
okini3939 0:f2bf5f966801 39 output[j+2] = (length>i+1)?base64[c]:'=';
okini3939 0:f2bf5f966801 40 c = (c3 & 0x3F);
okini3939 0:f2bf5f966801 41 output[j+3] = (length>i+2)?base64[c]:'=';
okini3939 0:f2bf5f966801 42 }
okini3939 0:f2bf5f966801 43 output[(((length-1)/3)+1)<<2] = '\0';
okini3939 2:764ecec3dc59 44 return 0;
okini3939 0:f2bf5f966801 45 }
okini3939 2:764ecec3dc59 46
okini3939 0:f2bf5f966801 47 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
okini3939 2:764ecec3dc59 48 int urlencode(char *str, char *buf, int len) {
okini3939 2:764ecec3dc59 49 static const char to_hex[] = "0123456789ABCDEF";
okini3939 0:f2bf5f966801 50 // char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
okini3939 0:f2bf5f966801 51 char *pstr = str, *pbuf = buf;
okini3939 2:764ecec3dc59 52
okini3939 2:764ecec3dc59 53 if (len < (strlen(str) * 3 + 1)) return -1;
okini3939 0:f2bf5f966801 54 while (*pstr) {
okini3939 2:764ecec3dc59 55 if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') {
okini3939 0:f2bf5f966801 56 *pbuf++ = *pstr;
okini3939 2:764ecec3dc59 57 } else if (*pstr == ' ') {
okini3939 0:f2bf5f966801 58 *pbuf++ = '+';
okini3939 2:764ecec3dc59 59 } else {
okini3939 2:764ecec3dc59 60 *pbuf++ = '%';
okini3939 2:764ecec3dc59 61 *pbuf++ = to_hex[(*pstr >> 4) & 0x0f];
okini3939 2:764ecec3dc59 62 *pbuf++ = to_hex[*pstr & 0x0f];
okini3939 2:764ecec3dc59 63 }
okini3939 0:f2bf5f966801 64 pstr++;
okini3939 0:f2bf5f966801 65 }
okini3939 0:f2bf5f966801 66 *pbuf = '\0';
okini3939 0:f2bf5f966801 67 return 0;
okini3939 0:f2bf5f966801 68 }
okini3939 2:764ecec3dc59 69
okini3939 0:f2bf5f966801 70
okini3939 0:f2bf5f966801 71 void isr_http (TCPSocketEvent e) {
okini3939 0:f2bf5f966801 72
okini3939 0:f2bf5f966801 73 #ifdef DEBUG
okini3939 0:f2bf5f966801 74 printf("tcp(%d)\r\n", e);
okini3939 0:f2bf5f966801 75 #endif
okini3939 0:f2bf5f966801 76 switch(e) {
okini3939 2:764ecec3dc59 77 case TCPSOCKET_CONNECTED:
okini3939 2:764ecec3dc59 78 tcp_ready = 1;
okini3939 0:f2bf5f966801 79 break;
okini3939 0:f2bf5f966801 80
okini3939 2:764ecec3dc59 81 case TCPSOCKET_READABLE: //Incoming data
okini3939 2:764ecec3dc59 82 tcp_readable = 1;
okini3939 2:764ecec3dc59 83 break;
okini3939 2:764ecec3dc59 84
okini3939 0:f2bf5f966801 85 case TCPSOCKET_WRITEABLE: //We can send data
okini3939 2:764ecec3dc59 86 tcp_writable = 1;
okini3939 0:f2bf5f966801 87 break;
okini3939 0:f2bf5f966801 88
okini3939 0:f2bf5f966801 89 case TCPSOCKET_CONTIMEOUT:
okini3939 0:f2bf5f966801 90 case TCPSOCKET_CONRST:
okini3939 0:f2bf5f966801 91 case TCPSOCKET_CONABRT:
okini3939 0:f2bf5f966801 92 case TCPSOCKET_ERROR:
okini3939 0:f2bf5f966801 93 case TCPSOCKET_DISCONNECTED:
okini3939 2:764ecec3dc59 94 tcp_ready = 0;
okini3939 0:f2bf5f966801 95 break;
okini3939 0:f2bf5f966801 96 }
okini3939 0:f2bf5f966801 97 }
okini3939 0:f2bf5f966801 98
okini3939 2:764ecec3dc59 99 void createauth (char *user, char *pwd, char *buf, int len) {
okini3939 0:f2bf5f966801 100 char tmp[80];
okini3939 0:f2bf5f966801 101
okini3939 2:764ecec3dc59 102 strncpy(buf, "Authorization: Basic ", len);
okini3939 2:764ecec3dc59 103 snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd);
okini3939 2:764ecec3dc59 104 base64enc(tmp, strlen(tmp), &buf[strlen(buf)], len - strlen(buf));
okini3939 2:764ecec3dc59 105 strncat(buf, "\r\n", len - strlen(buf));
okini3939 0:f2bf5f966801 106 }
okini3939 0:f2bf5f966801 107
okini3939 0:f2bf5f966801 108 void isr_dns (DNSReply r) {
okini3939 0:f2bf5f966801 109
okini3939 2:764ecec3dc59 110 #ifdef DEBUG
okini3939 2:764ecec3dc59 111 printf("dns(%d)\r\n", r);
okini3939 2:764ecec3dc59 112 #endif
okini3939 0:f2bf5f966801 113 if (DNS_FOUND) {
okini3939 0:f2bf5f966801 114 dns_status = 1;
okini3939 0:f2bf5f966801 115 } else {
okini3939 0:f2bf5f966801 116 dns_status = -1;
okini3939 0:f2bf5f966801 117 }
okini3939 0:f2bf5f966801 118 }
okini3939 0:f2bf5f966801 119
okini3939 0:f2bf5f966801 120 int httpRequest (int method, Host *host, char *uri, char *head, char *body) {
okini3939 0:f2bf5f966801 121 TCPSocketErr err;
okini3939 2:764ecec3dc59 122 Timer timeout;
okini3939 2:764ecec3dc59 123 char buf[1500];
okini3939 0:f2bf5f966801 124 int i, ret = -1;
okini3939 0:f2bf5f966801 125
okini3939 0:f2bf5f966801 126 http = new TCPSocket;
okini3939 2:764ecec3dc59 127 tcp_ready = 0;
okini3939 2:764ecec3dc59 128 tcp_readable = 0;
okini3939 2:764ecec3dc59 129 tcp_writable = 0;
okini3939 0:f2bf5f966801 130
okini3939 0:f2bf5f966801 131 http->setOnEvent(isr_http);
okini3939 0:f2bf5f966801 132
okini3939 0:f2bf5f966801 133 // connect
okini3939 0:f2bf5f966801 134 if (host->getIp().isNull()) {
okini3939 2:764ecec3dc59 135 // resolv
okini3939 0:f2bf5f966801 136 DNSRequest dns;
okini3939 0:f2bf5f966801 137 dns_status = 0;
okini3939 0:f2bf5f966801 138 dns.setOnReply(isr_dns);
okini3939 0:f2bf5f966801 139 if (dns.resolve(host) != DNS_OK) goto exit;
okini3939 2:764ecec3dc59 140 timeout.reset();
okini3939 2:764ecec3dc59 141 timeout.start();
okini3939 2:764ecec3dc59 142 while (timeout.read_ms() < HTTP_TIMEOUT) {
okini3939 0:f2bf5f966801 143 if (dns_status) break;
okini3939 0:f2bf5f966801 144 Net::poll();
okini3939 0:f2bf5f966801 145 }
okini3939 2:764ecec3dc59 146 timeout.stop();
okini3939 2:764ecec3dc59 147 if (dns_status <= 0) goto exit;
okini3939 2:764ecec3dc59 148 #ifdef DEBUG
okini3939 2:764ecec3dc59 149 printf("%s [%d.%d.%d.%d]\r\n", host->getName(), (unsigned char)host->getIp()[0], (unsigned char)host->getIp()[1], (unsigned char)host->getIp()[2], (unsigned char)host->getIp()[3]);
okini3939 2:764ecec3dc59 150 #endif
okini3939 0:f2bf5f966801 151 }
okini3939 0:f2bf5f966801 152 if (! host->getPort()) {
okini3939 0:f2bf5f966801 153 host->setPort(HTTP_PORT);
okini3939 0:f2bf5f966801 154 }
okini3939 0:f2bf5f966801 155 err = http->connect(*host);
okini3939 0:f2bf5f966801 156 if (err != TCPSOCKET_OK) goto exit;
okini3939 0:f2bf5f966801 157
okini3939 0:f2bf5f966801 158 // wait connect
okini3939 2:764ecec3dc59 159 timeout.reset();
okini3939 2:764ecec3dc59 160 timeout.start();
okini3939 2:764ecec3dc59 161 while (timeout.read_ms() < HTTP_TIMEOUT) {
okini3939 2:764ecec3dc59 162 if (tcp_ready) break;
okini3939 0:f2bf5f966801 163 Net::poll();
okini3939 0:f2bf5f966801 164 }
okini3939 2:764ecec3dc59 165 timeout.stop();
okini3939 2:764ecec3dc59 166 if (! tcp_ready) goto exit;
okini3939 0:f2bf5f966801 167
okini3939 0:f2bf5f966801 168 // send request
okini3939 0:f2bf5f966801 169 if (method == METHOD_POST) {
okini3939 0:f2bf5f966801 170 http->send("POST ", 5);
okini3939 0:f2bf5f966801 171 } else {
okini3939 0:f2bf5f966801 172 http->send("GET ", 4);
okini3939 0:f2bf5f966801 173 }
okini3939 0:f2bf5f966801 174 http->send(uri, strlen(uri));
okini3939 0:f2bf5f966801 175 http->send(" HTTP/1.1\r\nHost: ", 17);
okini3939 0:f2bf5f966801 176 http->send(host->getName(), strlen(host->getName()));
okini3939 0:f2bf5f966801 177 http->send("\r\n", 2);
okini3939 1:9f15e579d914 178 http->send("Connection: close\r\n", 19);
okini3939 0:f2bf5f966801 179 if (head) {
okini3939 0:f2bf5f966801 180 http->send(head, strlen(head));
okini3939 0:f2bf5f966801 181 }
okini3939 0:f2bf5f966801 182 if (method == METHOD_POST) {
okini3939 0:f2bf5f966801 183 sprintf(buf, "Content-Length: %d\r\n", strlen(body));
okini3939 0:f2bf5f966801 184 http->send(buf, strlen(buf));
okini3939 0:f2bf5f966801 185 }
okini3939 0:f2bf5f966801 186 http->send("\r\n", 2);
okini3939 0:f2bf5f966801 187
okini3939 0:f2bf5f966801 188 // post method
okini3939 0:f2bf5f966801 189 if (method == METHOD_POST && body) {
okini3939 0:f2bf5f966801 190 http->send(body, strlen(body));
okini3939 0:f2bf5f966801 191 }
okini3939 0:f2bf5f966801 192
okini3939 0:f2bf5f966801 193 // wait responce
okini3939 2:764ecec3dc59 194 timeout.reset();
okini3939 2:764ecec3dc59 195 timeout.start();
okini3939 2:764ecec3dc59 196 while (timeout.read_ms() < HTTP_TIMEOUT) {
okini3939 2:764ecec3dc59 197 if (tcp_readable) break;
okini3939 0:f2bf5f966801 198 Net::poll();
okini3939 0:f2bf5f966801 199 }
okini3939 2:764ecec3dc59 200 timeout.stop();
okini3939 2:764ecec3dc59 201 if (! tcp_readable) goto exit;
okini3939 0:f2bf5f966801 202
okini3939 0:f2bf5f966801 203 // recv responce
okini3939 2:764ecec3dc59 204 i = http->recv(buf, sizeof(buf) - 1);
okini3939 0:f2bf5f966801 205 buf[i] = 0;
okini3939 2:764ecec3dc59 206 if (i < sizeof(buf) - 1) tcp_readable = 0;
okini3939 2:764ecec3dc59 207 if (strncmp(buf, "HTTP/", 5) == 0) {
okini3939 0:f2bf5f966801 208 ret = atoi(&buf[9]);
okini3939 0:f2bf5f966801 209 }
okini3939 0:f2bf5f966801 210 #ifdef DEBUG
okini3939 0:f2bf5f966801 211 printf(buf);
okini3939 0:f2bf5f966801 212 #endif
okini3939 0:f2bf5f966801 213
okini3939 0:f2bf5f966801 214 // recv dummy
okini3939 2:764ecec3dc59 215 timeout.reset();
okini3939 2:764ecec3dc59 216 timeout.start();
okini3939 2:764ecec3dc59 217 while (timeout.read_ms() < HTTP_TIMEOUT) {
okini3939 2:764ecec3dc59 218 if (tcp_readable) {
okini3939 2:764ecec3dc59 219 i = http->recv(buf, sizeof(buf) - 1);
okini3939 2:764ecec3dc59 220 buf[i] = 0;
okini3939 2:764ecec3dc59 221 if (i < sizeof(buf) - 1) tcp_readable = 0;
okini3939 0:f2bf5f966801 222 #ifdef DEBUG
okini3939 0:f2bf5f966801 223 printf(buf);
okini3939 0:f2bf5f966801 224 #endif
okini3939 2:764ecec3dc59 225 timeout.reset();
okini3939 2:764ecec3dc59 226 } else
okini3939 2:764ecec3dc59 227 if (! tcp_ready) {
okini3939 0:f2bf5f966801 228 break;
okini3939 0:f2bf5f966801 229 }
okini3939 0:f2bf5f966801 230 Net::poll();
okini3939 0:f2bf5f966801 231 }
okini3939 2:764ecec3dc59 232 timeout.stop();
okini3939 0:f2bf5f966801 233
okini3939 0:f2bf5f966801 234 exit:
okini3939 0:f2bf5f966801 235 http->resetOnEvent();
okini3939 0:f2bf5f966801 236 http->close();
okini3939 0:f2bf5f966801 237 delete http;
okini3939 0:f2bf5f966801 238
okini3939 0:f2bf5f966801 239 return ret;
okini3939 0:f2bf5f966801 240 }