////////////////////////////////////////////////////////////////////////////////////////////////// // handle set GPIO pin from client // receive a json such as {"0":0, "1":0, "5":1} // return true if success or false if fail ////////////////////////////////////////////////////////////////////////////////////////////////// void ESP8266WebServerEx::handleSetGpio() { char *buf = (char*)malloc(8); int i; if ( !buf ) { send(200, m_jsonMime, "{\"status\":false}"); return; } for (i=0; i<MAX_PIN_NUMBER; i++) { delay(0); sprintf(buf,"%d",i); if ( hasArg(buf) ) { if ( atoi(arg(buf).c_str()) ) { digitalWrite(i,HIGH); } else{ digitalWrite(i,LOW); } } } // Check virtual pin for ARM/DISARM sprintf(buf,"%d",g_ARM_OFF_PIN); if ( hasArg(buf) ) { if ( atoi(arg(buf).c_str()) ) { g_ArmLogic.resetLogic(); } else{ g_ArmLogic.disableBell(); } } send(200, m_jsonMime, "{\"status\":true}"); free(buf); }
void ArgList::getOverlayedArgVal(glm::vec2* pResult, const string& sName, const string& sOverlay1, const string& sOverlay2, const string& sID) const { if (hasArg(sName)) { if (hasArg(sOverlay1) || hasArg(sOverlay2)) { throw (Exception(AVG_ERR_INVALID_ARGS, string("Duplicate node arguments (")+sName+" and "+ sOverlay1+","+sOverlay2+") for node '"+sID+"'")); } *pResult = getArgVal<glm::vec2>(sName); } }
void ESP8266WebServerEx::handleLogin(const char* login, size_t loginLength, const char* homePath) { hasLogin = true; String session = GetSessionId(); long tag = -1; bool validSession = false; if ( session.length()==32 ) validSession = g_Session.isValid(session,&tag); // if everything is OK and user already logged in if ( validSession && tag>=0 && tag<10 ) { redirect(homePath); return; } // if user never login before, just display a form with challenge string long challenge = random(11,0x7fffffff); if ( !hasArg("userid") || !hasArg("hashpw") ) { challengeString = createChallengeString(challenge); SetSessionId(g_Session.Create(challenge)); sendEx(200, "text/html", login, loginLength, &loginForm); return; } // user already provided userId and password String userId = arg("userid"); String hashPw = arg("hashpw"); challengeString = createChallengeString(tag); if ( userId==adminUser ) { if ( checkPassword(adminUser,g_ModuleSettings.GetLoginPassword(true).c_str(),challengeString,hashPw) ) { g_Session.SetTag(session,ADMIN_USER_ID); redirect(homePath); return; } } else if ( userId==normalUser) { if ( checkPassword(normalUser,g_ModuleSettings.GetLoginPassword(false).c_str(),challengeString,hashPw) ) { g_Session.SetTag(session,USER_USER_ID); redirect(homePath); return; } } // show the login form with invalid userid/password here challengeString = createChallengeString(challenge); SetSessionId(g_Session.Create(challenge)); sendEx(200, "text/html", login, loginLength, &loginFormWithError); }
////////////////////////////////////////////////////////////////////////////////////////////////// // handle get GPIO pin from client // receive a json such as {"0":0, "1":0, "5":1} // return json such as {"0":1,"2":0,"4":1} which reflect current status of gpio ////////////////////////////////////////////////////////////////////////////////////////////////// void ESP8266WebServerEx::handleGetGpio() { char *buf = (char*)malloc(16); String result = "{"; int i, n; if ( !buf ) { send(200, m_jsonMime, "{}"); return; } for (i=0; i<MAX_PIN_NUMBER; i++) { delay(0); sprintf(buf,"%d",i); if ( hasArg(buf) ) { if ( digitalRead(i)==HIGH ) n=1; else n=0; sprintf(buf,"\"%d\":%d",i,n); if ( result.length()>1 ) result += ","; result += buf; } } // virtual pin for ARM/DISARM sprintf(buf,",\"%d\":%d",g_ARM_OFF_PIN,g_ArmLogic.getOutputActive()); result += "}"; send(200, m_jsonMime, result); free(buf); }
std::string FunctionArguments::getBytes( const char *name, const std::string &default_value ) { if( hasArg( name ) ) { return getBytes( name ); } else { return default_value; } }
long FunctionArguments::getLong( const char *name, long default_value ) { if( hasArg( name ) ) { return getLong( name ); } else { return default_value; } }
int FunctionArguments::getInteger( const char *name, int default_value ) { if( hasArg( name ) ) { return getInteger( name ); } else { return default_value; } }
bool FunctionArguments::getBoolean( const char *name, bool default_value ) { if( hasArg( name ) ) { return getBoolean( name ); } else { return default_value; } }
svn_wc_conflict_choice_t FunctionArguments::getWcConflictChoice ( const char *choice_name, svn_wc_conflict_choice_t default_value ) { if( hasArg( choice_name ) ) { return getWcConflictChoice( choice_name ); } return default_value; }
svn_depth_t FunctionArguments::getDepth ( const char *depth_name, svn_depth_t default_value ) { if( hasArg( depth_name ) ) { return getDepth( depth_name ); } return default_value; }
svn_depth_t FunctionArguments::getDepth ( const char *depth_name, const char *recursive_name, svn_depth_t default_value, svn_depth_t recursive_true_value, svn_depth_t recursive_false_value ) { if( hasArg( recursive_name ) && hasArg( depth_name ) ) { std::string msg = m_function_name; msg += "() cannot mix "; msg += depth_name; msg += " and "; msg += recursive_name; throw Py::TypeError( msg ); } if( hasArg( recursive_name ) ) { if( getBoolean( recursive_name ) ) { return recursive_true_value; } else { return recursive_false_value; } } if( hasArg( depth_name ) ) { return getDepth( depth_name ); } return default_value; }
svn_opt_revision_t FunctionArguments::getRevision ( const char *name, svn_opt_revision_t default_value ) { if( hasArg( name ) ) { return getRevision( name ); } else { return default_value; } }
Py::Object FunctionArguments::getArg( const char *arg_name ) { if( !hasArg( arg_name ) ) { std::string msg = m_function_name; msg += "() internal error - getArg called twice or for option arg that is missing with bad arg_name: "; msg += arg_name; throw Py::AttributeError( msg ); } Py::Object arg = m_checked_args[ arg_name ]; // Make sure that each arg is only fetched once // to detect coding errors m_checked_args.delItem( arg_name ); return arg; }
svn_opt_revision_t FunctionArguments::getRevision ( const char *name, svn_opt_revision_kind default_value ) { if( hasArg( name ) ) { return getRevision( name ); } else { svn_opt_revision_t revision; revision.kind = default_value; if( revision.kind == svn_opt_revision_number ) revision.value.number = 1; return revision; } }
bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){ #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("Parse Form: Boundary: "); DEBUG_OUTPUT.print(boundary); DEBUG_OUTPUT.print(" Length: "); DEBUG_OUTPUT.println(len); #endif String line; int retry = 0; do { line = client.readStringUntil('\r'); ++retry; } while (line.length() == 0 && retry < 3); client.readStringUntil('\n'); //start reading the form if (line == ("--"+boundary)){ RequestArgument* postArgs = new RequestArgument[32]; int postArgsLen = 0; while(1){ String argName; String argValue; String argType; String argFilename; bool argIsFile = false; line = client.readStringUntil('\r'); client.readStringUntil('\n'); if (line.startsWith("Content-Disposition")){ int nameStart = line.indexOf('='); if (nameStart != -1){ argName = line.substring(nameStart+2); nameStart = argName.indexOf('='); if (nameStart == -1){ argName = argName.substring(0, argName.length() - 1); } else { argFilename = argName.substring(nameStart+2, argName.length() - 1); argName = argName.substring(0, argName.indexOf('"')); argIsFile = true; #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("PostArg FileName: "); DEBUG_OUTPUT.println(argFilename); #endif //use GET to set the filename if uploading using blob if (argFilename == "blob" && hasArg("filename")) argFilename = arg("filename"); } #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("PostArg Name: "); DEBUG_OUTPUT.println(argName); #endif argType = "text/plain"; line = client.readStringUntil('\r'); client.readStringUntil('\n'); if (line.startsWith("Content-Type")){ argType = line.substring(line.indexOf(':')+2); //skip next line client.readStringUntil('\r'); client.readStringUntil('\n'); } #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("PostArg Type: "); DEBUG_OUTPUT.println(argType); #endif if (!argIsFile){ while(1){ line = client.readStringUntil('\r'); client.readStringUntil('\n'); if (line.startsWith("--"+boundary)) break; if (argValue.length() > 0) argValue += "\n"; argValue += line; } #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("PostArg Value: "); DEBUG_OUTPUT.println(argValue); DEBUG_OUTPUT.println(); #endif RequestArgument& arg = postArgs[postArgsLen++]; arg.key = argName; arg.value = argValue; if (line == ("--"+boundary+"--")){ #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.println("Done Parsing POST"); #endif break; } } else { _currentUpload.status = UPLOAD_FILE_START; _currentUpload.name = argName; _currentUpload.filename = argFilename; _currentUpload.type = argType; _currentUpload.totalSize = 0; _currentUpload.currentSize = 0; #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("Start File: "); DEBUG_OUTPUT.print(_currentUpload.filename); DEBUG_OUTPUT.print(" Type: "); DEBUG_OUTPUT.println(_currentUpload.type); #endif if(_currentHandler && _currentHandler->canUpload(_currentUri)) _currentHandler->upload(*this, _currentUri, _currentUpload); _currentUpload.status = UPLOAD_FILE_WRITE; uint8_t argByte = _uploadReadByte(client); readfile: while(argByte != 0x0D){ if (!client.connected()) return _parseFormUploadAborted(); _uploadWriteByte(argByte); argByte = _uploadReadByte(client); } argByte = _uploadReadByte(client); if (!client.connected()) return _parseFormUploadAborted(); if (argByte == 0x0A){ argByte = _uploadReadByte(client); if (!client.connected()) return _parseFormUploadAborted(); if ((char)argByte != '-'){ //continue reading the file _uploadWriteByte(0x0D); _uploadWriteByte(0x0A); goto readfile; } else { argByte = _uploadReadByte(client); if (!client.connected()) return _parseFormUploadAborted(); if ((char)argByte != '-'){ //continue reading the file _uploadWriteByte(0x0D); _uploadWriteByte(0x0A); _uploadWriteByte((uint8_t)('-')); goto readfile; } } uint8_t endBuf[boundary.length()]; client.readBytes(endBuf, boundary.length()); if (strstr((const char*)endBuf, boundary.c_str()) != NULL){ if(_currentHandler && _currentHandler->canUpload(_currentUri)) _currentHandler->upload(*this, _currentUri, _currentUpload); _currentUpload.totalSize += _currentUpload.currentSize; _currentUpload.status = UPLOAD_FILE_END; if(_currentHandler && _currentHandler->canUpload(_currentUri)) _currentHandler->upload(*this, _currentUri, _currentUpload); #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("End File: "); DEBUG_OUTPUT.print(_currentUpload.filename); DEBUG_OUTPUT.print(" Type: "); DEBUG_OUTPUT.print(_currentUpload.type); DEBUG_OUTPUT.print(" Size: "); DEBUG_OUTPUT.println(_currentUpload.totalSize); #endif line = client.readStringUntil(0x0D); client.readStringUntil(0x0A); if (line == "--"){ #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.println("Done Parsing POST"); #endif break; } continue; } else { _uploadWriteByte(0x0D); _uploadWriteByte(0x0A); _uploadWriteByte((uint8_t)('-')); _uploadWriteByte((uint8_t)('-')); uint32_t i = 0; while(i < boundary.length()){ _uploadWriteByte(endBuf[i++]); } argByte = _uploadReadByte(client); goto readfile; } } else { _uploadWriteByte(0x0D); goto readfile; } break; } } } } int iarg; int totalArgs = ((32 - postArgsLen) < _currentArgCount)?(32 - postArgsLen):_currentArgCount; for (iarg = 0; iarg < totalArgs; iarg++){ RequestArgument& arg = postArgs[postArgsLen++]; arg.key = _currentArgs[iarg].key; arg.value = _currentArgs[iarg].value; } if (_currentArgs) delete[] _currentArgs; _currentArgs = new RequestArgument[postArgsLen]; for (iarg = 0; iarg < postArgsLen; iarg++){ RequestArgument& arg = _currentArgs[iarg]; arg.key = postArgs[iarg].key; arg.value = postArgs[iarg].value; } _currentArgCount = iarg; if (postArgs) delete[] postArgs; return true; } #ifdef DEBUG_ESP_HTTP_SERVER DEBUG_OUTPUT.print("Error: line: "); DEBUG_OUTPUT.println(line); #endif return false; }
bool CMDArgs::hasValue(const char* name) { return (hasArg(name) && m_pInternal->m_mArgv[name].size() > 0); }