bool DSPServer::crossCompileFactory(MHD_Connection* connection, dsp_server_connection_info* info) { dsp_factory* factory; if ((factory = info->crossCompileFactory(this, info->fAnswer))) { fFactories.insert(factory); // Return machine_code to client, and keep the new compiled target, so that is it "cached" #ifdef LLVM_DSP_FACTORY string machine_code = writeDSPFactoryToMachine(dynamic_cast<llvm_dsp_factory*>(factory), info->fTarget); dsp_factory* new_factory = readDSPFactoryFromMachine(machine_code, info->fTarget); #else string machine_code = writeInterpreterDSPFactoryToMachine(dynamic_cast<interpreter_dsp_factory*>(factory)); dsp_factory* new_factory = readInterpreterDSPFactoryFromMachine(machine_code); #endif if (new_factory) { fFactories.insert(new_factory); if (fCreateDSPFactoryCb) { // Possibly call callback fCreateDSPFactoryCb(new_factory, fCreateDSPFactoryCb_arg); } } return sendPage(connection, machine_code, MHD_HTTP_OK, "text/html"); } else { return sendPage(connection, info->fAnswer, MHD_HTTP_BAD_REQUEST, "text/html"); } }
bool DSPServer::deleteFactory(MHD_Connection* connection, dsp_server_connection_info* info) { // Returns the factory (with incremented reference counter) #ifdef LLVM_DSP_FACTORY dsp_factory* factory = getDSPFactoryFromSHAKey(info->fSHAKey); #else dsp_factory* factory = getInterpreterDSPFactoryFromSHAKey(info->fSHAKey); #endif if (factory) { // Remove from the list fFactories.erase(factory); // Has to be done twice since getDSPFactoryFromSHAKey has incremented once more... #ifdef LLVM_DSP_FACTORY deleteDSPFactory(dynamic_cast<llvm_dsp_factory*>(factory)); deleteDSPFactory(dynamic_cast<llvm_dsp_factory*>(factory)); #else deleteInterpreterDSPFactory(dynamic_cast<interpreter_dsp_factory*>(factory)); deleteInterpreterDSPFactory(dynamic_cast<interpreter_dsp_factory*>(factory)); #endif return sendPage(connection, "", MHD_HTTP_OK, "text/html"); } else { return sendPage(connection, builtError(ERROR_FACTORY_NOTFOUND), MHD_HTTP_BAD_REQUEST, "text/html"); } }
bool DSPServer::crossCompileFactory(MHD_Connection* connection, dsp_server_connection_info* info) { llvm_dsp_factory* factory; if ((factory = info->crossCompileFactory(this, info->fAnswer))) { fFactories.insert(factory); // Return machine_code to client string machine_code = writeDSPFactoryToMachine(factory, info->fTarget); //char* machine_code = writeCDSPFactoryToMachine(factory, info->fTarget.c_str()); // And keep the new compiled target, so that is it "cached" llvm_dsp_factory* new_factory = readDSPFactoryFromMachine(machine_code, info->fTarget); //llvm_dsp_factory* new_factory = readCDSPFactoryFromMachine(machine_code, info->fTarget.c_str()); //freeCDSP(machine_code); // TODO if (new_factory) { fFactories.insert(new_factory); if (fCreateDSPFactoryCb) { // Possibly call callback fCreateDSPFactoryCb(new_factory, fCreateDSPFactoryCb_arg); } } //return sendPage(connection, writeDSPFactoryToMachine(factory, ""), MHD_HTTP_OK, "text/html"); return sendPage(connection, machine_code, MHD_HTTP_OK, "text/html"); } else { return sendPage(connection, info->fAnswer, MHD_HTTP_BAD_REQUEST, "text/html"); } }
bool DSPServer::stop(MHD_Connection* connection, dsp_server_connection_info* info) { if (stop(info->fInstanceKey)) { return sendPage(connection, "", MHD_HTTP_OK, "text/html"); } else { return sendPage(connection, builtError(ERROR_INSTANCE_NOTFOUND), MHD_HTTP_BAD_REQUEST, "text/html"); } }
bool DSPServer::getFactoryFromSHAKey(MHD_Connection* connection, dsp_server_connection_info* info) { if (info->getFactoryFromSHAKey(this)) { return sendPage(connection, info->fAnswer, MHD_HTTP_OK, "application/json"); } else { return sendPage(connection, info->fAnswer, MHD_HTTP_BAD_REQUEST, "text/html"); } }
bool DSPServer::deleteInstance(MHD_Connection* connection, dsp_server_connection_info* info) { if (deleteInstance(info->fInstanceKey)) { return sendPage(connection, "", MHD_HTTP_OK, "text/html"); } else { return sendPage(connection, info->fAnswer, MHD_HTTP_BAD_REQUEST, "text/html"); } }
bool DSPServer::createFactory(MHD_Connection* connection, dsp_server_connection_info* info) { dsp_factory* factory; if (info->getFactoryFromSHAKey(this)) { return sendPage(connection, info->fAnswer, MHD_HTTP_OK, "application/json"); } else if ((factory = info->createFactory(this, info->fAnswer))) { info->getJson(factory); fFactories.insert(factory); return sendPage(connection, info->fAnswer, MHD_HTTP_OK, "application/json"); } else { return sendPage(connection, info->fAnswer, MHD_HTTP_BAD_REQUEST, "text/html"); } }
// Response to all POST request // 3 requests are correct : // - /CreateFactory --> Receive Faust code / Compile Data / Send back JSON Interface // - /CreateInstance --> Receive factoryIndex / Create instance // - /DeleteFactory --> Receive factoryIndex / Delete factory int DSPServer::answerPost(MHD_Connection* connection, const char* url, const char* upload_data, size_t* upload_data_size, void** con_cls) { dsp_server_connection_info* info = (dsp_server_connection_info*)*con_cls; if (*upload_data_size != 0) { return info->postProcess(upload_data, upload_data_size); } else { if (strcmp(url, "/CreateFactory") == 0) { return createFactory(connection, info); } else if (strcmp(url, "/CrossCompileFactory") == 0) { return crossCompileFactory(connection, info); } else if (strcmp(url, "/GetFactoryFromSHAKey") == 0) { return getFactoryFromSHAKey(connection, info); } else if(strcmp(url, "/DeleteFactory") == 0) { return deleteFactory(connection, info); } else if (strcmp(url, "/CreateInstance") == 0) { return createInstance(connection, info); } else if (strcmp(url, "/DeleteInstance") == 0) { return deleteInstance(connection, info); } else if (strcmp(url, "/StartInstance") == 0) { return start(connection, info); } else if(strcmp(url, "/StopInstance") == 0) { return stop(connection, info); } else { return sendPage(connection, "", MHD_HTTP_BAD_REQUEST, "text/html"); } } }
int AmmBusEthernetProtocol::receiveEthernetRequests(ETHER_28J60 * e, DS3231 *clock, RTCDateTime * dt) { char* params; if (params = e->serviceRequest()) { //Serial.print("!"); if (strcmp(params,"state.html") == 0) { sendState(e,dt); } else { sendPage(e); } byte port; byte i; if (strcmp(params,"?all=off") == 0) { for (i=0; i<8; i++) { port=2+i; digitalWrite(port, HIGH); return 1; } } else if (strcmp(params,"?all=on") == 0) { for (i=0; i<8; i++) { port=2+i; digitalWrite(port, LOW); return 1; } } else { for (i=0; i<8; i++) { onStr[1] = 'a'+i; offStr[1] = 'a'+i; port=2+i; if (strcmp(params, offStr) == 0) { digitalWrite(port, HIGH); return 1; } else if (strcmp(params, onStr) == 0) { digitalWrite(port, LOW); return 1; } } } } return 0; }
bool DSPServer::deleteFactory(MHD_Connection* connection, dsp_server_connection_info* info) { // Returns the factory (with incremented reference counter) llvm_dsp_factory* factory = getDSPFactoryFromSHAKey(info->fSHAKey); //llvm_dsp_factory* factory = getCDSPFactoryFromSHAKey(info->fSHAKey.c_str()); if (factory) { // Remove from the list fFactories.erase(factory); // Has to be done twice since getDSPFactoryFromSHAKey has incremented once more... deleteDSPFactory(factory); deleteDSPFactory(factory); return sendPage(connection, "", MHD_HTTP_OK, "text/html"); } else { return sendPage(connection, builtError(ERROR_FACTORY_NOTFOUND), MHD_HTTP_BAD_REQUEST, "text/html"); } }
bool DSPServer::getAvailableFactories(MHD_Connection* connection) { stringstream answer; for (FactoryTableIt it = fFactories.begin(); it != fFactories.end(); it++) { dsp_factory* factory = *it; //answer << factory->getName() << ":" << factory->getTarget() << " " << factory->getSHAKey() << " "; answer << factory->getName() << " " << factory->getSHAKey() << " "; } return sendPage(connection, answer.str(), MHD_HTTP_OK, "text/plain"); }
// For now GET is not a request supported for now int DSPServer::answerGet(MHD_Connection* connection, const char* url) { if (strcmp(url, "/") == 0) { return sendPage(connection, pathToContent("remote-server.html"), MHD_HTTP_OK, "text/html"); } else if (strcmp(url, "/GetAvailableFactories") == 0) { return getAvailableFactories(connection); } else { return MHD_NO; } }
boolean WebServer::loop () { WebClient *c = netint -> processPacket (); if (c != NULL) { // Got a client with a request, process it DPRINT (F("Request for \"")); DPRINT (c -> request.url); DPRINTLN (F("\"")); sendPage (c); } return c != NULL; }
void generatePage(char *page) { char tmp[1024], data[65536], userPath[2048]; FILE *file = NULL; data[0] = '\0'; file = fopen(page, "r"); if(file == NULL) { fprintf(stderr, "hey mec j'trouve pas la page\n"); file = fopen("/var/www/404.html", "r"); if(file == NULL) { fprintf(stderr, "La page 404 n'a pas été trouvée\n"); return; } } while(fgets(tmp, sizeof(char) * 1024, file) != NULL) { if(strstr(tmp, "<filelist>") != NULL) { sprintf(userPath, "/ESIEACloud/%s", actualSession->login); strcat(data, print_folder(userPath)); } else if(strstr(tmp, "</filelist>") != NULL) { } else { strcat(data, tmp); } } sprintf(actualSession->header, "Content-type: text/html\r\nContent-length: %d\r\n\r\n", strlen(data)); sendHeader(); sendPage(data); }
/* * Handles high-level server communications */ void server_task_impl() { // Get the connection's app state uip_tcp_appstate_t *app = &(uip_conn->appstate); if (uip_connected()) { if (verbose) { Serial.println("Server connected"); } // Initialize the server request data app->ackedCount = 0; app->request = NULL; } if (uip_newdata()) { setRXPin(HIGH); // Process the received packet and check if a valid GET request had been received if (processPacket((char*)uip_appdata, uip_datalen()) && app->request) { if (verbose) { Serial.print("Processing request for "); Serial.println((char*)app->request); } sendPage(); } } // Did we get an ack for the last packet? if (uip_acked()) { // Record the bytes that were successfully sent app->ackedCount += app->sentCount; app->sentCount = 0; // Check if we're done or need to send more content for this // request if (app->ackedCount == (int)app->cursor) { // Done with the current request and connection uip_close(); } else { // Generate the content again to send the next packet of data sendPage(); } } // Check if we need to retransmit if (uip_rexmit()) { // Send the same data again (same ackedCount value) sendPage(); } if (uip_aborted() || uip_closed() || uip_timedout()) { // Check if a URL was stored for this connection if (app->request != NULL) { if (verbose) { Serial.println("Server connection closed"); } // Free RAM and clear the pointer free(app->request); app->request = NULL; } } }
/* * Handles high-level server communications */ void server_task_impl() { // Get the connection's app state uip_tcp_appstate_t *app = &(uip_conn->appstate); if (uip_connected()) { #ifdef DEBUG DebugPrintF(f_sc); // JM "Server connected"; #endif // Initialize the server request data app->ackedCount = 0; app->request = NULL; } if (uip_newdata()) { setRXPin(HIGH); // Process the received packet and check if a valid GET request had been received if (processPacket((char*)uip_appdata, uip_datalen()) && app->request) { #ifdef DEBUG DebugPrintFO(f_prf); // JM "Processing request for "; DebugPrint((char*)app->request); #endif sendPage(); } } // Did we get an ack for the last packet? if (uip_acked()) { // Record the bytes that were successfully sent app->ackedCount += app->sentCount; app->sentCount = 0; // Check if we're done or need to send more content for this // request if (app->ackedCount == (int)app->cursor) { // Done with the current request and connection uip_close(); } else { // Generate the content again to send the next packet of data sendPage(); } } // Check if we need to retransmit if (uip_rexmit()) { // Send the same data again (same ackedCount value) sendPage(); } if (uip_aborted() || uip_closed() || uip_timedout()) { // Check if a URL was stored for this connection if (app->request != NULL) { #ifdef DEBUG DebugPrintF(f_scc); // "Server connection closed" #endif // Free RAM and clear the pointer // GregEigsti - jrwifi submitted WiServer stability fix // free(app->request); app->request = NULL; } } }
char SC_Web_Server::on_receive() { Flio_TCP_Socket::on_receive(); if (!available()) return 1; if (pState==WEB_IDLE) { // start receiving a client request pBufferPos = recv(pBuffer, 4096); pBuffer[pBufferPos] = 0; if (strncmp(pBuffer, "GET ", 4)==0) { // client requests some document pState = WEB_CMD_GET; } else if (strncmp(pBuffer, "POST ", 5)==0) { // client sends a file pState = WEB_CMD_POST; } else { // FIXME: Zombie! } } else { // append more data if available int n = available(), max = 4096-pBufferPos; if (n>max) n = max; pBufferPos += recv(pBuffer+pBufferPos, n); pBuffer[pBufferPos] = 0; } if (pState==WEB_CMD_GET || pState==WEB_CMD_POST) { // find the magic empty line (header must be less than sizeof(pBuffer) const char *emptyLine = strstr(pBuffer, "\r\n\r\n"); if (!emptyLine) emptyLine = strstr(pBuffer, "\n\n"); if (emptyLine) { int nHeader = int(emptyLine-pBuffer) + ((*emptyLine=='\r')?4:2); pBuffer[nHeader-1]=0; printf("<---- POST: \n%s\n--->\n", pBuffer); if (pState==WEB_CMD_GET) { sendPage(pBuffer+4); pState = WEB_IDLE; } else if (pState==WEB_CMD_POST) { // TODO: interprete the client request const char *cn = strstr(pBuffer, "Content-Length: "); if (cn) { pUploadRemaining = atoi(cn+16); printf("Content Length: >>%d<<\n", (int)pUploadRemaining); } else { pUploadRemaining=0; } const char *bd = strstr(pBuffer, "boundary="); if (bd) { const char *bde = strstr(bd+9, "\r\n"); if (bde) { size_t len = bde-bd-9; strcpy(pBoundary, "\r\n--"); memcpy(pBoundary+4, bd+9, len); pBoundary[len+4] = 0; printf("Boundary found: >>%s<<\n", pBoundary); strcat(pBoundary, "--\r\n"); } } pState = WEB_UPLOAD_HEADER; } // move the remaining part of the buffer to the buffer start int nData = pBufferPos - nHeader; memmove(pBuffer, pBuffer+nHeader, nData); pBufferPos = nData; } } if (pState==WEB_UPLOAD_HEADER) { const char *emptyLine = strstr(pBuffer, "\r\n\r\n"); if (!emptyLine) emptyLine = strstr(pBuffer, "\n\n"); if (emptyLine) { int nHeader = int(emptyLine-pBuffer) + ((*emptyLine=='\r')?4:2); pBuffer[nHeader-1]=0; printf("<---- POST content: \n%s\n--->\n", pBuffer); // TODO: interprete the content header const char *fn = strstr(pBuffer, "filename=\""); if (fn) { const char *fne = strstr(fn+10, "\""); if (fne) { size_t len = fne-fn-10; memcpy(pFilename, fn+10, len); pFilename[len] = 0; printf("Filename found: >>%s<<\n", pFilename); } } // move the remaining part of the buffer to the buffer start int nData = pBufferPos - nHeader; memmove(pBuffer, pBuffer+nHeader, nData); pBufferPos = nData; pState = WEB_UPLOAD_DATA; // TODO: open file char buf[2048]; strcpy(buf, gUploadPath); strcat(buf, pFilename); pFile = fopen(buf, "wb"); } } if (pState==WEB_UPLOAD_DATA) { pBuffer[pBufferPos] = 0; int n = pBufferPos; int nbd = (int)strlen(pBoundary); //const char *bd = strstr(pBuffer, pBoundary); const char *bd = (char*)memmem(pBuffer, pBufferPos, pBoundary, nbd); if (bd) { n = int(bd-pBuffer); //printf("End found\n"); } else if (n<nbd) { //printf("Less than bd (%d)\n", n); const char *safe = strchr(pBuffer, '\r'); if (safe) n = int(safe-pBuffer); } else { //printf("More than bd (%d)\n", n); const char *safe = strchr(pBuffer+pBufferPos-nbd, '\r'); //if (safe) printf(">>%s<<\n", safe); if (safe) n = int(safe-pBuffer); } // copy bytes to file //printf("Copying %d bytes\n", n); if (n>0) fwrite(pBuffer, 1, n, pFile); if (n<pBufferPos) { int nHeader = n; int nData = pBufferPos - nHeader; memmove(pBuffer, pBuffer+nHeader, nData); pBufferPos = nData; pBuffer[pBufferPos] = 0; } else { pBufferPos = 0; } if (bd) { // close file fclose(pFile); sendHTML("<html><body><h1>Thank you!</h1></body></html>"); pState = WEB_IDLE; pBufferPos = 0; } } return 1; // start is "POST /upload " // wait for line "Content-Type:", then find "boundary=" in line, key is the remainder of the line // wait for line "Content-Length:", then get the number in bytes // wait for line "--" + key // wait for line "Content-Disposition:", find "filename=", get filename // wait for empty line // read 'length' bytes // wait for line "--" + key + "--" // reply, so sender know we are fine /* POST /upload HTTP/1.1 Host: localhost:8080 Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7BlGom6EjFm9Oydz Origin: http://localhost:8080 Accept-Encoding: gzip, deflate Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,x/x;q=0.8 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4 Referer: http://localhost:8080/upload Content-Length: 231 Accept-Language: en-us ------WebKitFormBoundary7BlGom6EjFm9Oydz Content-Disposition: form-data; name="fileID"; filename="00test.txt" Content-Type: text/plain The quick brown fox jumps over the lazy dog. ------WebKitFormBoundary7BlGom6EjFm9Oydz-- */ }