void CGI::processCGIData() { QString request_method; // retrieve request method request_method = getenv("REQUEST_METHOD"); // what is the request method (POST and GET supported) if (request_method == "POST") { // determine the length of the data QString content_length_str = getenv("CONTENT_LENGTH"); int content_length = content_length_str.toInt(); // allocate a buffer to hold the query char* query_str = new char[content_length + 1]; // open the data stream Q3TextStream postStream(stdin, QIODevice::ReadOnly); // set the encoding (required for readRawBytes to work) postStream.setEncoding(Q3TextStream::Latin1); // read the data into the allocated buffer postStream.readRawBytes(query_str, content_length); // make sure to NULL terminate the string query_str[content_length] = '\0'; // store the data query_string = query_str; // delete the temporary buffer delete query_str; } else if (request_method == "GET") { query_string = getenv("QUERY_STRING"); } else query_string = ""; // if there is a query string, proces the arguments from it. if (!query_string.isEmpty()) { QString name = ""; QString value = ""; int index = 0; int oldindex = 0; // frequently used regular expressions QRegExp paramEndExp("[&;]"); QRegExp paramPlusExp("\\+"); // iterate over the string finding name/value pairs do { // find the end of the parameter name (ends at '=') index = query_string.find('=', oldindex); if (index == -1) break; // extract the name from the query string name = query_string.mid(oldindex, (index - oldindex)); // replace +'s with spaces name.replace(paramPlusExp, " "); // unescape the string name = unescapeURL(name); // the new place to search from oldindex = index + 1; // find the end of the parameter pair (ends at '&') index = query_string.find(paramEndExp, oldindex); // extract the value from the query_string if (index != -1) value = query_string.mid(oldindex, (index - oldindex)); else value = query_string.mid(oldindex, (query_string.length() - oldindex)); // replace +'s with spaces value.replace(paramPlusExp, " "); // unescape the string value = unescapeURL(value); // the new oldindex oldindex = index + 1; if (value.isNull()) value = ""; // insert the parameter into the parameter list cgiParams.append(new CGIParam(name, value)); } while (index != -1); // while not out of parameters } }
int main() { setbuf(stdout, NULL); // no buffering for this filehandle char json[1024]; int content_length; int response_code; char buf[1024]; float amb_temp; setupEthernet(); // set time lcd.printf("Reading Time... "); ntp.setTime("time-c.nist.gov"); lcd.printf("Time set"); while(1) { // Loop char streamName[] = "amb-temp"; // Set value for stream you are using // GET Stream value response_code = getStream(streamName, json); content_length = readContentLength(json); pc.printf("GET Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json); Thread::wait(5000); ///// // PUT value to Strem amb_temp = tmp.read(); sprintf(json, "{\"value\":\"%0.2f\"}", amb_temp); response_code = putStream(streamName, json); content_length = readContentLength(json); pc.printf("PUT Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json); Thread::wait(5000); ////// // POST value(s) to Stream time_t seconds; seconds = time(NULL); // get current time from mbed RTC strftime(buf,40, "%Y-%m-%dT%H:%M:%S%z", localtime(&seconds)); amb_temp = tmp.read(); sprintf(json, "{ \"values\": [ {\"at\": \"%s\", \"value\":\"%0.2f\"} ] }", buf, amb_temp); response_code = postStream(streamName, json); content_length = readContentLength(json); pc.printf("POST Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json); Thread::wait(5000); /////// // PUT Location to Feed sprintf(json, "{ \"name\": \"%s\", \"latitude\": \"%0.8f\", \"longitude\": \"%0.8f\", \"elevation\": \"%0.2f\" }", LOC_NAME, LOC_LAT, LOC_LONG, LOC_ELEV); response_code = putLocation(json); content_length = readContentLength(json); pc.printf("PUT Location\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json); Thread::wait(5000); /////// // GET Location of Feed response_code = getLocation(json); content_length = readContentLength(json); pc.printf("GET Location\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json); Thread::wait(5000); } eth.disconnect(); while(1) {} }