phs fan
/
PHSShield_test
see: https://developer.mbed.org/users/phsfan/notebook/phsshield/
Embed:
(wiki syntax)
Show/hide line numbers
TinyHTTP_a3gs.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 "TinyHTTP_a3gs.h" 00013 #include "a3gs.h" 00014 #include <ctype.h> 00015 00016 //#define DEBUG 00017 00018 extern A3GS a3gs; 00019 static onHttpReceiveFunc onHttpReceive = NULL; 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[1000]; 00079 int i, ret = -1; 00080 00081 onHttpReceive = func; 00082 00083 if (a3gs.connectTCP(host, port)) return -1; 00084 00085 #ifdef DEBUG 00086 printf("connected\r\n"); 00087 #endif 00088 00089 // send request 00090 if (method == METHOD_POST) { 00091 a3gs.write((uint8_t*)"POST ", 5); 00092 } else { 00093 a3gs.write((uint8_t*)"GET ", 4); 00094 } 00095 a3gs.write((uint8_t*)uri, strlen(uri)); 00096 a3gs.write((uint8_t*)" HTTP/1.1\r\nHost: ", 17); 00097 a3gs.write((uint8_t*)host, strlen(host)); 00098 a3gs.write((uint8_t*)"\r\n", 2); 00099 a3gs.write((uint8_t*)"Connection: close\r\n", 19); 00100 if (head) { 00101 a3gs.write((uint8_t*)head, strlen(head)); 00102 } 00103 if (method == METHOD_POST) { 00104 sprintf(buf, "Content-Length: %d\r\n", strlen(body)); 00105 a3gs.write((uint8_t*)buf, strlen(buf)); 00106 } 00107 a3gs.write((uint8_t*)"\r\n", 2); 00108 00109 // post method 00110 if (method == METHOD_POST && body) { 00111 a3gs.write((uint8_t*)body, strlen(body)); 00112 } 00113 00114 printf("wait for response\r\n"); 00115 // recv 00116 int err = 0; 00117 for (;;) { 00118 i = a3gs.read(buf, sizeof(buf) - 1); 00119 if (i <= 0) break; 00120 00121 if (onHttpReceive != NULL) onHttpReceive(buf, i); 00122 } 00123 ret = 0; 00124 00125 a3gs.disconnectTCP(); 00126 00127 return ret; 00128 }
Generated on Mon Jul 18 2022 13:29:08 by 1.7.2