Fixed a bug
This commit is contained in:
		
							parent
							
								
									ad39aafa89
								
							
						
					
					
						commit
						672aeb6670
					
				
					 5 changed files with 130 additions and 37 deletions
				
			
		|  | @ -16,32 +16,20 @@ void HTTP::Parser::Clean(){ | |||
|   protocol = "HTTP/1.1"; | ||||
|   body.clear(); | ||||
|   length = 0; | ||||
|   HTTPbuffer.clear(); | ||||
|   headers.clear(); | ||||
|   vars.clear(); | ||||
| } | ||||
| 
 | ||||
| /// Re-initializes the HTTP::Parser, leaving the internal data buffer alone, then tries to parse a new request or response.
 | ||||
| /// First does the same as HTTP::Parser::Clean(), but does not clear the internal data buffer.
 | ||||
| /// This function then calls the HTTP::Parser::parse() function, and returns that functions return value.
 | ||||
| /// Does the same as HTTP::Parser::Clean(), then returns HTTP::Parser::parse().
 | ||||
| bool HTTP::Parser::CleanForNext(){ | ||||
|   seenHeaders = false; | ||||
|   seenReq = false; | ||||
|   method = "GET"; | ||||
|   url = "/"; | ||||
|   protocol = "HTTP/1.1"; | ||||
|   body = ""; | ||||
|   length = 0; | ||||
|   headers.clear(); | ||||
|   vars.clear(); | ||||
|   Clean(); | ||||
|   return parse(); | ||||
| } | ||||
| 
 | ||||
| /// Returns a string containing a valid HTTP 1.0 or 1.1 request, ready for sending.
 | ||||
| /// The request is build from internal variables set before this call is made.
 | ||||
| /// To be precise, method, url, protocol, headers and the internal data buffer are used,
 | ||||
| /// where the internal data buffer is used as the body of the request.
 | ||||
| /// This means you cannot mix receiving and sending, because the body would get corrupted.
 | ||||
| /// To be precise, method, url, protocol, headers and body are used.
 | ||||
| /// \return A string containing a valid HTTP 1.0 or 1.1 request, ready for sending.
 | ||||
| std::string HTTP::Parser::BuildRequest(){ | ||||
|   /// \todo Include GET/POST variable parsing?
 | ||||
|  | @ -51,15 +39,13 @@ std::string HTTP::Parser::BuildRequest(){ | |||
|     tmp += (*it).first + ": " + (*it).second + "\n"; | ||||
|   } | ||||
|   tmp += "\n"; | ||||
|   tmp += HTTPbuffer; | ||||
|   tmp += body; | ||||
|   return tmp; | ||||
| } | ||||
| 
 | ||||
| /// Returns a string containing a valid HTTP 1.0 or 1.1 response, ready for sending.
 | ||||
| /// The response is partly build from internal variables set before this call is made.
 | ||||
| /// To be precise, protocol, headers and the internal data buffer are used,
 | ||||
| /// where the internal data buffer is used as the body of the response.
 | ||||
| /// This means you cannot mix receiving and sending, because the body would get corrupted.
 | ||||
| /// To be precise, protocol, headers and body are used.
 | ||||
| /// \param code The HTTP response code. Usually you want 200.
 | ||||
| /// \param message The HTTP response message. Usually you want "OK".
 | ||||
| /// \return A string containing a valid HTTP 1.0 or 1.1 response, ready for sending.
 | ||||
|  | @ -71,7 +57,7 @@ std::string HTTP::Parser::BuildResponse(std::string code, std::string message){ | |||
|     tmp += (*it).first + ": " + (*it).second + "\n"; | ||||
|   } | ||||
|   tmp += "\n"; | ||||
|   tmp += HTTPbuffer; | ||||
|   tmp += body; | ||||
|   return tmp; | ||||
| } | ||||
| 
 | ||||
|  | @ -87,7 +73,7 @@ void HTTP::Parser::Trim(std::string & s){ | |||
| /// Function that sets the body of a response or request, along with the correct Content-Length header.
 | ||||
| /// \param s The string to set the body to.
 | ||||
| void HTTP::Parser::SetBody(std::string s){ | ||||
|   HTTPbuffer = s; | ||||
|   body = s; | ||||
|   SetHeader("Content-Length", s.length()); | ||||
| } | ||||
| 
 | ||||
|  | @ -95,8 +81,8 @@ void HTTP::Parser::SetBody(std::string s){ | |||
| /// \param buffer The buffer data to set the body to.
 | ||||
| /// \param len Length of the buffer data.
 | ||||
| void HTTP::Parser::SetBody(char * buffer, int len){ | ||||
|   HTTPbuffer = ""; | ||||
|   HTTPbuffer.append(buffer, len); | ||||
|   body = ""; | ||||
|   body.append(buffer, len); | ||||
|   SetHeader("Content-Length", len); | ||||
| } | ||||
| 
 | ||||
|  | @ -265,8 +251,8 @@ void HTTP::Parser::SendBodyPart(Socket::Connection & conn, std::string bodypart) | |||
|   } | ||||
| } | ||||
| 
 | ||||
| /// Unescapes URLencoded std::strings.
 | ||||
| std::string HTTP::Parser::urlunescape(std::string in){ | ||||
| /// Unescapes URLencoded std::string data.
 | ||||
| std::string HTTP::Parser::urlunescape(const std::string & in){ | ||||
|   std::string out; | ||||
|   for (unsigned int i = 0; i < in.length(); ++i){ | ||||
|     if (in[i] == '%'){ | ||||
|  | @ -292,3 +278,33 @@ std::string HTTP::Parser::urlunescape(std::string in){ | |||
| int HTTP::Parser::unhex(char c){ | ||||
|   return( c >= '0' && c <= '9' ? c - '0' : c >= 'A' && c <= 'F' ? c - 'A' + 10 : c - 'a' + 10 ); | ||||
| } | ||||
| 
 | ||||
| /// URLencodes std::string data.
 | ||||
| std::string HTTP::Parser::urlencode(const std::string &c){ | ||||
|   std::string escaped=""; | ||||
|   int max = c.length(); | ||||
|   for(int i=0; i<max; i++){ | ||||
|     if (('0'<=c[i] && c[i]<='9') || ('a'<=c[i] && c[i]<='z') || ('A'<=c[i] && c[i]<='Z') || (c[i]=='~' || c[i]=='!' || c[i]=='*' || c[i]=='(' || c[i]==')' || c[i]=='\'')){ | ||||
|       escaped.append( &c[i], 1); | ||||
|     }else{ | ||||
|       escaped.append("%"); | ||||
|       escaped.append(hex(c[i])); | ||||
|     } | ||||
|   } | ||||
|   return escaped; | ||||
| } | ||||
| 
 | ||||
| /// Helper function for urlescape.
 | ||||
| /// Encodes a character as two hex digits.
 | ||||
| std::string HTTP::Parser::hex(char dec){ | ||||
|   char dig1 = (dec&0xF0)>>4; | ||||
|   char dig2 = (dec&0x0F); | ||||
|   if (dig1<= 9) dig1+=48; | ||||
|   if (10<= dig1 && dig1<=15) dig1+=97-10; | ||||
|   if (dig2<= 9) dig2+=48; | ||||
|   if (10<= dig2 && dig2<=15) dig2+=97-10; | ||||
|   std::string r; | ||||
|   r.append(&dig1, 1); | ||||
|   r.append(&dig2, 1); | ||||
|   return r; | ||||
| } | ||||
|  |  | |||
|  | @ -30,7 +30,8 @@ namespace HTTP{ | |||
|       void SendBodyPart(Socket::Connection & conn, std::string bodypart); | ||||
|       void Clean(); | ||||
|       bool CleanForNext(); | ||||
|       std::string urlunescape(std::string in); | ||||
|       static std::string urlunescape(const std::string & in); | ||||
|       static std::string urlencode(const std::string & in); | ||||
|       std::string body; | ||||
|       std::string method; | ||||
|       std::string url; | ||||
|  | @ -45,6 +46,7 @@ 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.
 | ||||
|       static int unhex(char c); | ||||
|       static std::string hex(char dec); | ||||
|   };//HTTP::Parser class
 | ||||
| };//HTTP namespace
 | ||||
|  |  | |||
|  | @ -159,10 +159,10 @@ int main(int argc, char ** argv){ | |||
|   if (server_socket.connected()){ | ||||
|     //if setup success, enter daemon mode if requested
 | ||||
|     if (daemon_mode){ | ||||
|       daemon(1, 0); | ||||
|       #if DEBUG >= 3 | ||||
|       fprintf(stderr, "Going into background mode...\n"); | ||||
|       #endif | ||||
|       daemon(1, 0); | ||||
|     } | ||||
|   }else{ | ||||
|     #if DEBUG >= 1 | ||||
|  |  | |||
|  | @ -1,17 +1,19 @@ | |||
| /// \file util.cpp
 | ||||
| /// Contains generic functions for managing processes and configuration.
 | ||||
| 
 | ||||
| #include "proc.h" | ||||
| #include "util.h" | ||||
| #include <string.h> | ||||
| #include <sys/types.h> | ||||
| #include <signal.h> | ||||
| #include <wait.h> | ||||
| #include <errno.h> | ||||
| #if DEBUG >= 1 | ||||
| #include <iostream> | ||||
| #endif | ||||
| #include <sys/types.h> | ||||
| #include <pwd.h> | ||||
| #include <getopt.h> | ||||
| #include <stdlib.h> | ||||
| #include <stdio.h> | ||||
| #include <fstream> | ||||
| 
 | ||||
| std::map<pid_t, std::string> Util::Procs::plist; | ||||
| bool Util::Procs::handler_set = false; | ||||
|  | @ -24,7 +26,7 @@ void Util::setUser(std::string username){ | |||
|       #if DEBUG >= 1 | ||||
|       fprintf(stderr, "Error: could not setuid %s: could not get PID\n", username.c_str()); | ||||
|       #endif | ||||
|       return 1; | ||||
|       return; | ||||
|     }else{ | ||||
|       if (setuid(user_info->pw_uid) != 0){ | ||||
|         #if DEBUG >= 1 | ||||
|  | @ -260,6 +262,77 @@ Util::Config::Config(){ | |||
|   ignore_user = false; | ||||
| } | ||||
| 
 | ||||
| void parseArgs(int argc, char ** argv){ | ||||
|    | ||||
| /// Parses commandline arguments.
 | ||||
| /// Calls exit if an unknown option is encountered, printing a help message.
 | ||||
| /// Assumes confsection is set.
 | ||||
| void Util::Config::parseArgs(int argc, char ** argv){ | ||||
|   int opt = 0; | ||||
|   static const char *optString = "ndp:i:u:c:h?"; | ||||
|   static const struct option longOpts[] = { | ||||
|     {"help",0,0,'h'}, | ||||
|     {"port",1,0,'p'}, | ||||
|     {"interface",1,0,'i'}, | ||||
|     {"username",1,0,'u'}, | ||||
|     {"no-daemon",0,0,'n'}, | ||||
|     {"daemon",0,0,'d'}, | ||||
|     {"configfile",1,0,'c'} | ||||
|   }; | ||||
|   while ((opt = getopt_long(argc, argv, optString, longOpts, 0)) != -1){ | ||||
|     switch (opt){ | ||||
|       case 'p': listen_port = atoi(optarg); ignore_port = true; break; | ||||
|       case 'i': interface = optarg; ignore_interface = true; break; | ||||
|       case 'n': daemon_mode = false; ignore_daemon = true; break; | ||||
|       case 'd': daemon_mode = true; ignore_daemon = true; break; | ||||
|       case 'c': configfile = optarg; break; | ||||
|       case 'u': username = optarg; ignore_user = true; break; | ||||
|       case 'h': | ||||
|       case '?': | ||||
|         printf("Options: -h[elp], -?, -n[odaemon], -d[aemon], -p[ort] VAL, -i[nterface] VAL, -c[onfigfile] VAL, -u[sername] VAL\n"); | ||||
|         printf("Defaults:\n  interface: 0.0.0.0\n  port: %i\n  daemon mode: true\n  configfile: /etc/ddvtech.conf\n  username: root\n", listen_port); | ||||
|         printf("Username root means no change to UID, no matter what the UID is.\n"); | ||||
|         printf("If the configfile exists, it is always loaded first. Commandline settings then overwrite the config file.\n"); | ||||
|         printf("\nThis process takes it directives from the %s section of the configfile.\n", confsection.c_str()); | ||||
|         exit(1); | ||||
|         break; | ||||
|     } | ||||
|   }//commandline options parser
 | ||||
| } | ||||
| 
 | ||||
| /// Parses the configuration file at configfile, if it exists.
 | ||||
| /// Assumes confsection is set.
 | ||||
| void Util::Config::parseFile(){ | ||||
|   std::ifstream conf(configfile.c_str(), std::ifstream::in); | ||||
|   std::string tmpstr; | ||||
|   bool acc_comm = false; | ||||
|   size_t foundeq; | ||||
|   if (conf.fail()){ | ||||
|     #if DEBUG >= 3 | ||||
|     fprintf(stderr, "Configuration file %s not found - using build-in defaults...\n", configfile.c_str()); | ||||
|     #endif | ||||
|   }else{ | ||||
|     while (conf.good()){ | ||||
|       getline(conf, tmpstr); | ||||
|       if (tmpstr[0] == '['){//new section? check if we care.
 | ||||
|         if (tmpstr == confsection){acc_comm = true;}else{acc_comm = false;} | ||||
|       }else{ | ||||
|         if (!acc_comm){break;}//skip all lines in this section if we do not care about it
 | ||||
|         foundeq = tmpstr.find('='); | ||||
|         if (foundeq != std::string::npos){ | ||||
|           if ((tmpstr.substr(0, foundeq) == "port") && !ignore_port){listen_port = atoi(tmpstr.substr(foundeq+1).c_str());} | ||||
|           if ((tmpstr.substr(0, foundeq) == "interface") && !ignore_interface){interface = tmpstr.substr(foundeq+1);} | ||||
|           if ((tmpstr.substr(0, foundeq) == "username") && !ignore_user){username = tmpstr.substr(foundeq+1);} | ||||
|           if ((tmpstr.substr(0, foundeq) == "daemon") && !ignore_daemon){daemon_mode = true;} | ||||
|           if ((tmpstr.substr(0, foundeq) == "nodaemon") && !ignore_daemon){daemon_mode = false;} | ||||
|         }//found equals sign
 | ||||
|       }//section contents
 | ||||
|     }//configfile line loop
 | ||||
|   }//configuration
 | ||||
| } | ||||
| 
 | ||||
| /// Will turn the current process into a daemon.
 | ||||
| /// Works by calling daemon(1,0):
 | ||||
| /// Does not change directory to root.
 | ||||
| /// Does redirect output to /dev/null
 | ||||
| void Util::Daemonize(){ | ||||
|   daemon(1, 0); | ||||
| } | ||||
|  | @ -29,7 +29,7 @@ namespace Util{ | |||
|   }; | ||||
| 
 | ||||
|   /// Will set the active user to the named username.
 | ||||
|   static setUser(std::string user); | ||||
|   void setUser(std::string user); | ||||
| 
 | ||||
|   /// Deals with parsing configuration from files or commandline options.
 | ||||
|   class Config{ | ||||
|  | @ -39,6 +39,7 @@ namespace Util{ | |||
|     bool ignore_port; | ||||
|     bool ignore_user; | ||||
|   public: | ||||
|     std::string confsection; | ||||
|     std::string configfile; | ||||
|     bool daemon_mode; | ||||
|     std::string interface; | ||||
|  | @ -46,6 +47,7 @@ namespace Util{ | |||
|     std::string username; | ||||
|     Config(); | ||||
|     void parseArgs(int argc, char ** argv); | ||||
|     void parseFile(); | ||||
|   }; | ||||
| 
 | ||||
|   /// Will turn the current process into a daemon.
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Thulinma
						Thulinma