HTTP Client test for AbitUSBModem. see: http://developer.mbed.org/users/phsfan/notebook/abitusbmodem/

Dependencies:   AbitUSBModem USBHost mbed

Committer:
phsfan
Date:
Thu Feb 19 00:08:49 2015 +0000
Revision:
0:35cf1b2fc6e2
1st build;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phsfan 0:35cf1b2fc6e2 1 /*
phsfan 0:35cf1b2fc6e2 2 * mbed Tiny HTTP Client for Ethernet Interface Library
phsfan 0:35cf1b2fc6e2 3 * Copyright (c) 2012 Hiroshi Suga
phsfan 0:35cf1b2fc6e2 4 * Released under the MIT License: http://mbed.org/license/mit
phsfan 0:35cf1b2fc6e2 5 */
phsfan 0:35cf1b2fc6e2 6
phsfan 0:35cf1b2fc6e2 7 /** @file
phsfan 0:35cf1b2fc6e2 8 * @brief Tiny HTTP Client
phsfan 0:35cf1b2fc6e2 9 */
phsfan 0:35cf1b2fc6e2 10
phsfan 0:35cf1b2fc6e2 11 #include "mbed.h"
phsfan 0:35cf1b2fc6e2 12 #include "TinyHTTP.h"
phsfan 0:35cf1b2fc6e2 13 #include "TCPSocketConnection.h"
phsfan 0:35cf1b2fc6e2 14 #include <ctype.h>
phsfan 0:35cf1b2fc6e2 15
phsfan 0:35cf1b2fc6e2 16 //#define DEBUG
phsfan 0:35cf1b2fc6e2 17
phsfan 0:35cf1b2fc6e2 18 static onHttpReceiveFunc onHttpReceive = NULL;
phsfan 0:35cf1b2fc6e2 19
phsfan 0:35cf1b2fc6e2 20 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
phsfan 0:35cf1b2fc6e2 21 int base64enc(const char *input, unsigned int length, char *output, int len) {
phsfan 0:35cf1b2fc6e2 22 static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
phsfan 0:35cf1b2fc6e2 23 unsigned int c, c1, c2, c3;
phsfan 0:35cf1b2fc6e2 24
phsfan 0:35cf1b2fc6e2 25 if (len < ((((length-1)/3)+1)<<2)) return -1;
phsfan 0:35cf1b2fc6e2 26 for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) {
phsfan 0:35cf1b2fc6e2 27 c1 = ((((unsigned char)*((unsigned char *)&input[i]))));
phsfan 0:35cf1b2fc6e2 28 c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0;
phsfan 0:35cf1b2fc6e2 29 c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0;
phsfan 0:35cf1b2fc6e2 30
phsfan 0:35cf1b2fc6e2 31 c = ((c1 & 0xFC) >> 2);
phsfan 0:35cf1b2fc6e2 32 output[j+0] = base64[c];
phsfan 0:35cf1b2fc6e2 33 c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4);
phsfan 0:35cf1b2fc6e2 34 output[j+1] = base64[c];
phsfan 0:35cf1b2fc6e2 35 c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6);
phsfan 0:35cf1b2fc6e2 36 output[j+2] = (length>i+1)?base64[c]:'=';
phsfan 0:35cf1b2fc6e2 37 c = (c3 & 0x3F);
phsfan 0:35cf1b2fc6e2 38 output[j+3] = (length>i+2)?base64[c]:'=';
phsfan 0:35cf1b2fc6e2 39 }
phsfan 0:35cf1b2fc6e2 40 output[(((length-1)/3)+1)<<2] = '\0';
phsfan 0:35cf1b2fc6e2 41 return 0;
phsfan 0:35cf1b2fc6e2 42 }
phsfan 0:35cf1b2fc6e2 43
phsfan 0:35cf1b2fc6e2 44 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
phsfan 0:35cf1b2fc6e2 45 int urlencode(char *str, char *buf, int len) {
phsfan 0:35cf1b2fc6e2 46 static const char to_hex[] = "0123456789ABCDEF";
phsfan 0:35cf1b2fc6e2 47 // char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
phsfan 0:35cf1b2fc6e2 48 char *pstr = str, *pbuf = buf;
phsfan 0:35cf1b2fc6e2 49
phsfan 0:35cf1b2fc6e2 50 if (len < (strlen(str) * 3 + 1)) return -1;
phsfan 0:35cf1b2fc6e2 51 while (*pstr) {
phsfan 0:35cf1b2fc6e2 52 if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') {
phsfan 0:35cf1b2fc6e2 53 *pbuf++ = *pstr;
phsfan 0:35cf1b2fc6e2 54 } else if (*pstr == ' ') {
phsfan 0:35cf1b2fc6e2 55 *pbuf++ = '+';
phsfan 0:35cf1b2fc6e2 56 } else {
phsfan 0:35cf1b2fc6e2 57 *pbuf++ = '%';
phsfan 0:35cf1b2fc6e2 58 *pbuf++ = to_hex[(*pstr >> 4) & 0x0f];
phsfan 0:35cf1b2fc6e2 59 *pbuf++ = to_hex[*pstr & 0x0f];
phsfan 0:35cf1b2fc6e2 60 }
phsfan 0:35cf1b2fc6e2 61 pstr++;
phsfan 0:35cf1b2fc6e2 62 }
phsfan 0:35cf1b2fc6e2 63 *pbuf = '\0';
phsfan 0:35cf1b2fc6e2 64 return 0;
phsfan 0:35cf1b2fc6e2 65 }
phsfan 0:35cf1b2fc6e2 66
phsfan 0:35cf1b2fc6e2 67 void createauth (char *user, char *pwd, char *buf, int len) {
phsfan 0:35cf1b2fc6e2 68 char tmp[80];
phsfan 0:35cf1b2fc6e2 69
phsfan 0:35cf1b2fc6e2 70 strncpy(buf, "Authorization: Basic ", len);
phsfan 0:35cf1b2fc6e2 71 snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd);
phsfan 0:35cf1b2fc6e2 72 base64enc(tmp, strlen(tmp), &buf[strlen(buf)], len - strlen(buf));
phsfan 0:35cf1b2fc6e2 73 strncat(buf, "\r\n", len - strlen(buf));
phsfan 0:35cf1b2fc6e2 74 }
phsfan 0:35cf1b2fc6e2 75
phsfan 0:35cf1b2fc6e2 76 int httpRequest (int method, char *host, int port, char *uri, char *head, char *body, onHttpReceiveFunc func) {
phsfan 0:35cf1b2fc6e2 77 char buf[1000];
phsfan 0:35cf1b2fc6e2 78 int i, ret = -1;
phsfan 0:35cf1b2fc6e2 79
phsfan 0:35cf1b2fc6e2 80 onHttpReceive = func;
phsfan 0:35cf1b2fc6e2 81
phsfan 0:35cf1b2fc6e2 82 TCPSocketConnection socket;
phsfan 0:35cf1b2fc6e2 83 if (socket.connect(host, port) < 0) goto exit;
phsfan 0:35cf1b2fc6e2 84
phsfan 0:35cf1b2fc6e2 85 #ifdef DEBUG
phsfan 0:35cf1b2fc6e2 86 printf("connected\r\n");
phsfan 0:35cf1b2fc6e2 87 #endif
phsfan 0:35cf1b2fc6e2 88
phsfan 0:35cf1b2fc6e2 89 // send request
phsfan 0:35cf1b2fc6e2 90 if (method == METHOD_POST) {
phsfan 0:35cf1b2fc6e2 91 socket.send_all("POST ", 5);
phsfan 0:35cf1b2fc6e2 92 } else {
phsfan 0:35cf1b2fc6e2 93 socket.send_all("GET ", 4);
phsfan 0:35cf1b2fc6e2 94 }
phsfan 0:35cf1b2fc6e2 95 socket.send_all(uri, strlen(uri));
phsfan 0:35cf1b2fc6e2 96 socket.send_all(" HTTP/1.1\r\nHost: ", 17);
phsfan 0:35cf1b2fc6e2 97 socket.send_all(host, strlen(host));
phsfan 0:35cf1b2fc6e2 98 socket.send_all("\r\n", 2);
phsfan 0:35cf1b2fc6e2 99 socket.send_all("Connection: close\r\n", 19);
phsfan 0:35cf1b2fc6e2 100 if (head) {
phsfan 0:35cf1b2fc6e2 101 socket.send_all(head, strlen(head));
phsfan 0:35cf1b2fc6e2 102 }
phsfan 0:35cf1b2fc6e2 103 if (method == METHOD_POST) {
phsfan 0:35cf1b2fc6e2 104 sprintf(buf, "Content-Length: %d\r\n", strlen(body));
phsfan 0:35cf1b2fc6e2 105 socket.send_all(buf, strlen(buf));
phsfan 0:35cf1b2fc6e2 106 }
phsfan 0:35cf1b2fc6e2 107 socket.send_all("\r\n", 2);
phsfan 0:35cf1b2fc6e2 108
phsfan 0:35cf1b2fc6e2 109 // post method
phsfan 0:35cf1b2fc6e2 110 if (method == METHOD_POST && body) {
phsfan 0:35cf1b2fc6e2 111 socket.send_all(body, strlen(body));
phsfan 0:35cf1b2fc6e2 112 }
phsfan 0:35cf1b2fc6e2 113
phsfan 0:35cf1b2fc6e2 114 printf("wait for response\r\n");
phsfan 0:35cf1b2fc6e2 115 // recv
phsfan 0:35cf1b2fc6e2 116 for (;;) {
phsfan 0:35cf1b2fc6e2 117 i = socket.receive(buf, sizeof(buf) - 1);
phsfan 0:35cf1b2fc6e2 118 if (i == 0) break;
phsfan 0:35cf1b2fc6e2 119
phsfan 0:35cf1b2fc6e2 120 if (onHttpReceive != NULL) onHttpReceive(buf, i);
phsfan 0:35cf1b2fc6e2 121 }
phsfan 0:35cf1b2fc6e2 122 ret = 0;
phsfan 0:35cf1b2fc6e2 123
phsfan 0:35cf1b2fc6e2 124 exit:
phsfan 0:35cf1b2fc6e2 125 socket.close();
phsfan 0:35cf1b2fc6e2 126
phsfan 0:35cf1b2fc6e2 127 return ret;
phsfan 0:35cf1b2fc6e2 128 }