Added POST var parsing to HTTP lib, added WriteFile to JSON gearbox version, added real parsing of input to JSON gearbox version
This commit is contained in:
		
							parent
							
								
									86e7d69e09
								
							
						
					
					
						commit
						57b9866b4b
					
				
					 4 changed files with 44 additions and 27 deletions
				
			
		| 
						 | 
				
			
			@ -188,10 +188,25 @@ bool HTTP::Parser::parse(){
 | 
			
		|||
    }
 | 
			
		||||
    if (seenHeaders){
 | 
			
		||||
      if (length > 0){
 | 
			
		||||
        /// \todo Include POST variable parsing?
 | 
			
		||||
        if (HTTPbuffer.length() >= length){
 | 
			
		||||
          body = HTTPbuffer.substr(0, length);
 | 
			
		||||
          HTTPbuffer.erase(0, length);
 | 
			
		||||
          std::string tmppost = body;
 | 
			
		||||
          std::string varname;
 | 
			
		||||
          std::string varval;
 | 
			
		||||
          while (tmppost.find('=') != std::string::npos){
 | 
			
		||||
            size_t found = tmppost.find('=');
 | 
			
		||||
            varname = urlunescape(tmppost.substr(0, found).c_str());
 | 
			
		||||
            tmppost.erase(0, found+1);
 | 
			
		||||
            size_t found = tmppost.find('&');
 | 
			
		||||
            varval = urlunescape(tmppost.substr(0, found).c_str());
 | 
			
		||||
            SetVar(varname, varval);
 | 
			
		||||
            if (found == std::string::npos){
 | 
			
		||||
              tmppost.clear();
 | 
			
		||||
            }else{
 | 
			
		||||
              tmppost.erase(0, found+1);
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
          return true;
 | 
			
		||||
        }else{
 | 
			
		||||
          return false;
 | 
			
		||||
| 
						 | 
				
			
			@ -241,3 +256,29 @@ void HTTP::Parser::SendBodyPart(Socket::Connection & conn, std::string bodypart)
 | 
			
		|||
    conn.write(bodypart);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Unescapes URLencoded C-strings to a std::string.
 | 
			
		||||
/// This function *will* destroy the input data!
 | 
			
		||||
std::string HTTP::Parser::urlunescape(char *s){
 | 
			
		||||
  char  *p;
 | 
			
		||||
  for (p = s; *s != '\0'; ++s){
 | 
			
		||||
    if (*s == '%'){
 | 
			
		||||
      if (*++s != '\0'){
 | 
			
		||||
        *p = unhex(*s) << 4;
 | 
			
		||||
      }
 | 
			
		||||
      if (*++s != '\0'){
 | 
			
		||||
        *p++ += unhex(*s);
 | 
			
		||||
      }
 | 
			
		||||
    } else {
 | 
			
		||||
      if (*s == '+'){*p++ = ' ';}else{*p++ = *s;}
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  *p = '\0';
 | 
			
		||||
  return std::string(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Helper function for urlunescape.
 | 
			
		||||
/// Takes a single char input and outputs its integer hex value.
 | 
			
		||||
int HTTP::Parser::unhex(char c){
 | 
			
		||||
  return( c >= '0' && c <= '9' ? c - '0' : c >= 'A' && c <= 'F' ? c - 'A' + 10 : c - 'a' + 10 );
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -30,6 +30,7 @@ namespace HTTP{
 | 
			
		|||
      void SendBodyPart(Socket::Connection & conn, std::string bodypart);
 | 
			
		||||
      void Clean();
 | 
			
		||||
      bool CleanForNext();
 | 
			
		||||
      std::string urlunescape(char *s); ///< Unescapes URLencoded C-strings to a std::string.
 | 
			
		||||
      std::string body;
 | 
			
		||||
      std::string method;
 | 
			
		||||
      std::string url;
 | 
			
		||||
| 
						 | 
				
			
			@ -43,5 +44,6 @@ namespace HTTP{
 | 
			
		|||
      std::map<std::string, std::string> headers;
 | 
			
		||||
      std::map<std::string, std::string> vars;
 | 
			
		||||
      void Trim(std::string & s);
 | 
			
		||||
      int unhex(char c); ///< Helper function for urlunescape.
 | 
			
		||||
  };//HTTP::Parser class
 | 
			
		||||
};//HTTP namespace
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -495,27 +495,3 @@ bool Socket::Server::connected(){
 | 
			
		|||
 | 
			
		||||
/// Returns internal socket number.
 | 
			
		||||
int Socket::Server::getSocket(){return sock;}
 | 
			
		||||
 | 
			
		||||
/// Unescapes URLencoded C-strings to a std::string.
 | 
			
		||||
/// This function *will* destroy the incoming data!
 | 
			
		||||
std::string Socket::Connection::urlunescape(char *s){
 | 
			
		||||
  char  *p;
 | 
			
		||||
  for (p = s; *s != '\0'; ++s){
 | 
			
		||||
    if (*s == '%'){
 | 
			
		||||
      if (*++s != '\0'){
 | 
			
		||||
        *p = unhex(*s) << 4;
 | 
			
		||||
      }
 | 
			
		||||
      if (*++s != '\0'){
 | 
			
		||||
        *p++ += unhex(*s);
 | 
			
		||||
      }
 | 
			
		||||
    } else {
 | 
			
		||||
      if (*s == '+'){*p++ = ' ';}else{*p++ = *s;}
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  *p = '\0';
 | 
			
		||||
  return std::string(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int Socket::Connection::unhex(char c){
 | 
			
		||||
  return( c >= '0' && c <= '9' ? c - '0' : c >= 'A' && c <= 'F' ? c - 'A' + 10 : c - 'a' + 10 );
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,7 +23,6 @@ namespace Socket{
 | 
			
		|||
    private:
 | 
			
		||||
      int sock; ///< Internally saved socket number.
 | 
			
		||||
      std::string remotehost; ///< Stores remote host address.
 | 
			
		||||
      int unhex(char c); ///< Helper function for urlunescape.
 | 
			
		||||
    public:
 | 
			
		||||
      Connection(); ///< Create a new disconnected base socket.
 | 
			
		||||
      Connection(int sockNo); ///< Create a new base socket.
 | 
			
		||||
| 
						 | 
				
			
			@ -45,7 +44,6 @@ namespace Socket{
 | 
			
		|||
      bool swrite(std::string & buffer); ///< Read call that is compatible with std::string.
 | 
			
		||||
      bool iread(std::string & buffer); ///< Incremental write call that is compatible with std::string.
 | 
			
		||||
      bool iwrite(std::string & buffer); ///< Write call that is compatible with std::string.
 | 
			
		||||
      std::string urlunescape(char *s); ///< Unescapes URLencoded C-strings to a std::string.
 | 
			
		||||
      void close(); ///< Close connection.
 | 
			
		||||
      std::string getHost(); ///< Gets hostname for connection, if available.
 | 
			
		||||
      int getSocket(); ///< Returns internal socket number.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue