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