Kansai Electric Power usage meter (Denki-yohou)

Dependencies:   mbed mbed-rtos EthernetInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TinyHTTP_b.cpp Source File

TinyHTTP_b.cpp

Go to the documentation of this file.
00001 /*
00002  * mbed Tiny HTTP Client for Ethernet Interface Library
00003  * Copyright (c) 2012 Hiroshi Suga
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  */
00006 
00007 /** @file
00008  * @brief Tiny HTTP Client
00009  */
00010 
00011 #include "mbed.h"
00012 #include "EthernetInterface.h"
00013 #include "TinyHTTP_b.h"
00014 #include <ctype.h>
00015 
00016 //#define DEBUG
00017 
00018 static TCPSocket *http;
00019 static onHttpReceiveFunc onHttpReceive;
00020 
00021 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00022 int base64enc(const char *input, unsigned int length, char *output, int len) {
00023   static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00024   unsigned int c, c1, c2, c3;
00025 
00026   if (len < ((((length-1)/3)+1)<<2)) return -1;
00027   for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) {
00028     c1 = ((((unsigned char)*((unsigned char *)&input[i]))));
00029     c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0;
00030     c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0;
00031 
00032     c = ((c1 & 0xFC) >> 2);
00033     output[j+0] = base64[c];
00034     c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4);
00035     output[j+1] = base64[c];
00036     c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6);
00037     output[j+2] = (length>i+1)?base64[c]:'=';
00038     c = (c3 & 0x3F);
00039     output[j+3] = (length>i+2)?base64[c]:'=';
00040   }
00041   output[(((length-1)/3)+1)<<2] = '\0';
00042   return 0;
00043 }
00044 
00045 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00046 int urlencode(char *str, char *buf, int len) {
00047   static const char to_hex[] = "0123456789ABCDEF";
00048 //  char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
00049   char *pstr = str, *pbuf = buf;
00050 
00051   if (len < (strlen(str) * 3 + 1)) return -1;
00052   while (*pstr) {
00053     if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') {
00054       *pbuf++ = *pstr;
00055     } else if (*pstr == ' ') {
00056       *pbuf++ = '+';
00057     } else { 
00058       *pbuf++ = '%';
00059       *pbuf++ = to_hex[(*pstr >> 4) & 0x0f];
00060       *pbuf++ = to_hex[*pstr & 0x0f];
00061     }
00062     pstr++;
00063   }
00064   *pbuf = '\0';
00065   return 0;
00066 }
00067 
00068 void createauth (char *user, char *pwd, char *buf, int len) {
00069     char tmp[80];
00070 
00071     strncpy(buf, "Authorization: Basic ", len);
00072     snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd);
00073     base64enc(tmp, strlen(tmp), &buf[strlen(buf)], len - strlen(buf));
00074     strncat(buf, "\r\n", len - strlen(buf));
00075 }
00076 
00077 int httpRequest (int method, char *host, int port, char *uri, char *head, char *body, onHttpReceiveFunc func) {
00078     char buf[1500];
00079     int i, ret = -1;
00080 
00081     http = new TCPSocket;
00082     onHttpReceive = func;
00083     
00084     if (http->connect(host, port, HTTP_TIMEOUT)) goto exit;
00085 #ifdef DEBUG
00086         printf("connected\r\n");
00087 #endif
00088 
00089     // send request
00090     if (method == METHOD_POST) {
00091         http->send("POST ", 5, HTTP_TIMEOUT);
00092     } else {
00093         http->send("GET ", 4, HTTP_TIMEOUT);
00094     }
00095     http->send(uri, strlen(uri), HTTP_TIMEOUT);
00096     http->send(" HTTP/1.1\r\nHost: ", 17, HTTP_TIMEOUT);
00097     http->send(host, strlen(host), HTTP_TIMEOUT);
00098     http->send("\r\n", 2, HTTP_TIMEOUT);
00099     http->send("Connection: close\r\n", 19, HTTP_TIMEOUT);
00100     if (head) {
00101         http->send(head, strlen(head), HTTP_TIMEOUT);
00102     }
00103     if (method == METHOD_POST) {
00104         sprintf(buf, "Content-Length: %d\r\n", strlen(body));
00105         http->send(buf, strlen(buf), HTTP_TIMEOUT);
00106     }
00107     http->send("\r\n", 2, HTTP_TIMEOUT);
00108 
00109     // post method
00110     if (method == METHOD_POST && body) {
00111         http->send(body, strlen(body), HTTP_TIMEOUT);
00112     }
00113 
00114 #ifdef DEBUG
00115         printf("wait for response\r\n");
00116 #endif
00117     // recv
00118     for (;;) {
00119         i = http->receive(buf, sizeof(buf) - 1, HTTP_TIMEOUT);
00120         if (i == 0) break;
00121 
00122         if (onHttpReceive != NULL) onHttpReceive(buf, i);
00123 #ifdef DEBUG
00124         printf(buf);
00125 #endif
00126     }
00127     ret = 0;
00128 
00129 exit:
00130     http->close();
00131     delete http;
00132 
00133     return ret;
00134 }