private fork
Fork of GSwifiInterface by
GSwifi/GSwifi_http.cpp@8:64184a968e3b, 2013-10-31 (annotated)
- Committer:
- gsfan
- Date:
- Thu Oct 31 06:41:45 2013 +0000
- Revision:
- 8:64184a968e3b
- Child:
- 11:71d67fea5ace
supported mbed RTOS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsfan | 8:64184a968e3b | 1 | /* Copyright (C) 2013 gsfan, MIT License |
gsfan | 8:64184a968e3b | 2 | * |
gsfan | 8:64184a968e3b | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
gsfan | 8:64184a968e3b | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
gsfan | 8:64184a968e3b | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
gsfan | 8:64184a968e3b | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
gsfan | 8:64184a968e3b | 7 | * furnished to do so, subject to the following conditions: |
gsfan | 8:64184a968e3b | 8 | * |
gsfan | 8:64184a968e3b | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
gsfan | 8:64184a968e3b | 10 | * substantial portions of the Software. |
gsfan | 8:64184a968e3b | 11 | * |
gsfan | 8:64184a968e3b | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
gsfan | 8:64184a968e3b | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
gsfan | 8:64184a968e3b | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
gsfan | 8:64184a968e3b | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
gsfan | 8:64184a968e3b | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
gsfan | 8:64184a968e3b | 17 | */ |
gsfan | 8:64184a968e3b | 18 | |
gsfan | 8:64184a968e3b | 19 | #include "GSwifi.h" |
gsfan | 8:64184a968e3b | 20 | |
gsfan | 8:64184a968e3b | 21 | int GSwifi::httpGet (const char *host, int port, const char *uri, bool ssl, const char *user, const char *pwd) { |
gsfan | 8:64184a968e3b | 22 | char ip[17]; |
gsfan | 8:64184a968e3b | 23 | int cid; |
gsfan | 8:64184a968e3b | 24 | |
gsfan | 8:64184a968e3b | 25 | if (getHostByName(host, ip)) return -1; |
gsfan | 8:64184a968e3b | 26 | if (! port) { |
gsfan | 8:64184a968e3b | 27 | if (ssl) { |
gsfan | 8:64184a968e3b | 28 | port = 443; |
gsfan | 8:64184a968e3b | 29 | } else { |
gsfan | 8:64184a968e3b | 30 | port = 80; |
gsfan | 8:64184a968e3b | 31 | } |
gsfan | 8:64184a968e3b | 32 | } |
gsfan | 8:64184a968e3b | 33 | |
gsfan | 8:64184a968e3b | 34 | if (cmdHTTPCONF(3, "close")) return -1; // Connection |
gsfan | 8:64184a968e3b | 35 | cmdHTTPCONFDEL(5); |
gsfan | 8:64184a968e3b | 36 | cmdHTTPCONFDEL(7); |
gsfan | 8:64184a968e3b | 37 | cmdHTTPCONF(11, host); // Host |
gsfan | 8:64184a968e3b | 38 | if (user && pwd) { |
gsfan | 8:64184a968e3b | 39 | char tmp[CFG_CMD_SIZE], tmp2[CFG_CMD_SIZE]; |
gsfan | 8:64184a968e3b | 40 | snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); |
gsfan | 8:64184a968e3b | 41 | base64encode(tmp, strlen(tmp), tmp2, sizeof(tmp2)); |
gsfan | 8:64184a968e3b | 42 | sprintf(tmp, "Basic %s", tmp2); |
gsfan | 8:64184a968e3b | 43 | cmdHTTPCONF(2, tmp); // Authorization |
gsfan | 8:64184a968e3b | 44 | } else { |
gsfan | 8:64184a968e3b | 45 | cmdHTTPCONFDEL(2); |
gsfan | 8:64184a968e3b | 46 | } |
gsfan | 8:64184a968e3b | 47 | |
gsfan | 8:64184a968e3b | 48 | if (cmdHTTPOPEN(ip, port, ssl)) return -1; |
gsfan | 8:64184a968e3b | 49 | if (_state.cid < 0) return -1; |
gsfan | 8:64184a968e3b | 50 | cid = _state.cid; |
gsfan | 8:64184a968e3b | 51 | _con[cid].protocol = PROTO_HTTPGET; |
gsfan | 8:64184a968e3b | 52 | _con[cid].type = TYPE_CLIENT; |
gsfan | 8:64184a968e3b | 53 | |
gsfan | 8:64184a968e3b | 54 | cmdHTTPSEND(cid, false, uri); // GET |
gsfan | 8:64184a968e3b | 55 | return cid; |
gsfan | 8:64184a968e3b | 56 | } |
gsfan | 8:64184a968e3b | 57 | |
gsfan | 8:64184a968e3b | 58 | int GSwifi::httpPost (const char *host, int port, const char *uri, const char *body, bool ssl, const char *user, const char *pwd) { |
gsfan | 8:64184a968e3b | 59 | char cmd[CFG_CMD_SIZE]; |
gsfan | 8:64184a968e3b | 60 | char ip[17]; |
gsfan | 8:64184a968e3b | 61 | int cid, len; |
gsfan | 8:64184a968e3b | 62 | |
gsfan | 8:64184a968e3b | 63 | if (getHostByName(host, ip)) return -1; |
gsfan | 8:64184a968e3b | 64 | if (! port) { |
gsfan | 8:64184a968e3b | 65 | if (ssl) { |
gsfan | 8:64184a968e3b | 66 | port = 443; |
gsfan | 8:64184a968e3b | 67 | } else { |
gsfan | 8:64184a968e3b | 68 | port = 80; |
gsfan | 8:64184a968e3b | 69 | } |
gsfan | 8:64184a968e3b | 70 | } |
gsfan | 8:64184a968e3b | 71 | len = strlen(body); |
gsfan | 8:64184a968e3b | 72 | |
gsfan | 8:64184a968e3b | 73 | if (cmdHTTPCONF(3, "close")) return -1; // Connection |
gsfan | 8:64184a968e3b | 74 | sprintf(cmd, "%d", len); |
gsfan | 8:64184a968e3b | 75 | cmdHTTPCONF(5, cmd); // Content-Length |
gsfan | 8:64184a968e3b | 76 | cmdHTTPCONF(7, "application/x-www-form-urlencoded"); // Content-type |
gsfan | 8:64184a968e3b | 77 | cmdHTTPCONF(11, host); // Host |
gsfan | 8:64184a968e3b | 78 | if (user && pwd) { |
gsfan | 8:64184a968e3b | 79 | char tmp[CFG_CMD_SIZE], tmp2[CFG_CMD_SIZE]; |
gsfan | 8:64184a968e3b | 80 | snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); |
gsfan | 8:64184a968e3b | 81 | base64encode(tmp, strlen(tmp), tmp2, sizeof(tmp2)); |
gsfan | 8:64184a968e3b | 82 | sprintf(tmp, "Basic %s", tmp2); |
gsfan | 8:64184a968e3b | 83 | cmdHTTPCONF(2, tmp); // Authorization |
gsfan | 8:64184a968e3b | 84 | } else { |
gsfan | 8:64184a968e3b | 85 | cmdHTTPCONFDEL(2); |
gsfan | 8:64184a968e3b | 86 | } |
gsfan | 8:64184a968e3b | 87 | |
gsfan | 8:64184a968e3b | 88 | if (cmdHTTPOPEN(ip, port, ssl)) return -1; |
gsfan | 8:64184a968e3b | 89 | if (_state.cid < 0) return -1; |
gsfan | 8:64184a968e3b | 90 | cid = _state.cid; |
gsfan | 8:64184a968e3b | 91 | _con[cid].protocol = PROTO_HTTPPOST; |
gsfan | 8:64184a968e3b | 92 | _con[cid].type = TYPE_CLIENT; |
gsfan | 8:64184a968e3b | 93 | |
gsfan | 8:64184a968e3b | 94 | cmdHTTPSEND(cid, true, uri, len); // POST |
gsfan | 8:64184a968e3b | 95 | sprintf(cmd, "\x1bH%X", cid); |
gsfan | 8:64184a968e3b | 96 | sendData(body, len, DEFAULT_WAIT_RESP_TIMEOUT, cmd); |
gsfan | 8:64184a968e3b | 97 | return cid; |
gsfan | 8:64184a968e3b | 98 | } |
gsfan | 8:64184a968e3b | 99 | |
gsfan | 8:64184a968e3b | 100 | |
gsfan | 8:64184a968e3b | 101 | |
gsfan | 8:64184a968e3b | 102 | /* base64encode code from |
gsfan | 8:64184a968e3b | 103 | * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
gsfan | 8:64184a968e3b | 104 | */ |
gsfan | 8:64184a968e3b | 105 | int GSwifi::base64encode (const char *input, int length, char *output, int len) { |
gsfan | 8:64184a968e3b | 106 | static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
gsfan | 8:64184a968e3b | 107 | unsigned int c, c1, c2, c3; |
gsfan | 8:64184a968e3b | 108 | |
gsfan | 8:64184a968e3b | 109 | if (len < ((((length-1)/3)+1)<<2)) return -1; |
gsfan | 8:64184a968e3b | 110 | for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) { |
gsfan | 8:64184a968e3b | 111 | c1 = ((((unsigned char)*((unsigned char *)&input[i])))); |
gsfan | 8:64184a968e3b | 112 | c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0; |
gsfan | 8:64184a968e3b | 113 | c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0; |
gsfan | 8:64184a968e3b | 114 | |
gsfan | 8:64184a968e3b | 115 | c = ((c1 & 0xFC) >> 2); |
gsfan | 8:64184a968e3b | 116 | output[j+0] = base64[c]; |
gsfan | 8:64184a968e3b | 117 | c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4); |
gsfan | 8:64184a968e3b | 118 | output[j+1] = base64[c]; |
gsfan | 8:64184a968e3b | 119 | c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6); |
gsfan | 8:64184a968e3b | 120 | output[j+2] = (length>i+1)?base64[c]:'='; |
gsfan | 8:64184a968e3b | 121 | c = (c3 & 0x3F); |
gsfan | 8:64184a968e3b | 122 | output[j+3] = (length>i+2)?base64[c]:'='; |
gsfan | 8:64184a968e3b | 123 | } |
gsfan | 8:64184a968e3b | 124 | output[(((length-1)/3)+1)<<2] = '\0'; |
gsfan | 8:64184a968e3b | 125 | return 0; |
gsfan | 8:64184a968e3b | 126 | } |
gsfan | 8:64184a968e3b | 127 | |
gsfan | 8:64184a968e3b | 128 | |
gsfan | 8:64184a968e3b | 129 | /* urlencode code from |
gsfan | 8:64184a968e3b | 130 | * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
gsfan | 8:64184a968e3b | 131 | */ |
gsfan | 8:64184a968e3b | 132 | int GSwifi::urlencode (const char *str, char *buf, int len) { |
gsfan | 8:64184a968e3b | 133 | // char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf; |
gsfan | 8:64184a968e3b | 134 | const char *pstr = str; |
gsfan | 8:64184a968e3b | 135 | char *pbuf = buf; |
gsfan | 8:64184a968e3b | 136 | |
gsfan | 8:64184a968e3b | 137 | if (len < (strlen(str) * 3 + 1)) return -1; |
gsfan | 8:64184a968e3b | 138 | while (*pstr) { |
gsfan | 8:64184a968e3b | 139 | if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') |
gsfan | 8:64184a968e3b | 140 | *pbuf++ = *pstr; |
gsfan | 8:64184a968e3b | 141 | else if (*pstr == ' ') |
gsfan | 8:64184a968e3b | 142 | *pbuf++ = '+'; |
gsfan | 8:64184a968e3b | 143 | else |
gsfan | 8:64184a968e3b | 144 | *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); |
gsfan | 8:64184a968e3b | 145 | pstr++; |
gsfan | 8:64184a968e3b | 146 | } |
gsfan | 8:64184a968e3b | 147 | *pbuf = '\0'; |
gsfan | 8:64184a968e3b | 148 | return 0; |
gsfan | 8:64184a968e3b | 149 | } |
gsfan | 8:64184a968e3b | 150 | |
gsfan | 8:64184a968e3b | 151 | /* urldecode code from |
gsfan | 8:64184a968e3b | 152 | * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
gsfan | 8:64184a968e3b | 153 | */ |
gsfan | 8:64184a968e3b | 154 | int GSwifi::urldecode (const char *str, char *buf, int len) { |
gsfan | 8:64184a968e3b | 155 | // char *pstr = str, *buf = (char*)malloc(strlen(str) + 1), *pbuf = buf; |
gsfan | 8:64184a968e3b | 156 | const char *pstr = str; |
gsfan | 8:64184a968e3b | 157 | char *pbuf = buf; |
gsfan | 8:64184a968e3b | 158 | |
gsfan | 8:64184a968e3b | 159 | if (len < (strlen(str) / 3 - 1)) return -1; |
gsfan | 8:64184a968e3b | 160 | while (*pstr) { |
gsfan | 8:64184a968e3b | 161 | if (*pstr == '%') { |
gsfan | 8:64184a968e3b | 162 | if (pstr[1] && pstr[2]) { |
gsfan | 8:64184a968e3b | 163 | *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); |
gsfan | 8:64184a968e3b | 164 | pstr += 2; |
gsfan | 8:64184a968e3b | 165 | } |
gsfan | 8:64184a968e3b | 166 | } else if (*pstr == '+') { |
gsfan | 8:64184a968e3b | 167 | *pbuf++ = ' '; |
gsfan | 8:64184a968e3b | 168 | } else { |
gsfan | 8:64184a968e3b | 169 | *pbuf++ = *pstr; |
gsfan | 8:64184a968e3b | 170 | } |
gsfan | 8:64184a968e3b | 171 | pstr++; |
gsfan | 8:64184a968e3b | 172 | } |
gsfan | 8:64184a968e3b | 173 | *pbuf = '\0'; |
gsfan | 8:64184a968e3b | 174 | return 0; |
gsfan | 8:64184a968e3b | 175 | } |