Commit d6075159 authored by jan.koester's avatar jan.koester
Browse files

better header api

parent cd3c4787
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
 *******************************************************************************/

#include <algorithm>
#include <sstream>
#include <memory>
#include <chrono>
#include <thread>
@@ -85,10 +86,12 @@ int main(int argc, char** argv){
      req.setRequestType(GETREQUEST);
      req.setRequestURL(argv[3]);
      req.setRequestVersion(HTTPVERSION(1.1));
      *req.setData("connection") << "keep-alive";
      *req.setData("host") << argv[1] << ":" << argv[2];
      *req.setData("accept") << "text/html";
      *req.setData("user-agent") << "libhttppp/1.0 (Alpha Version 0.1)";
      req.setHeaderData("connection")->at(0)="keep-alive";
      std::stringstream shost;
      shost << argv[1] << ':' << argv[2];
      req.setHeaderData("host")->at(0)= shost.str().c_str();
      req.setHeaderData("accept")->at(0)="text/html";
      req.setHeaderData("user-agent")->at(0)="libhttppp/1.0 (Alpha Version 0.1)";
      req.send(&srvsock);
    }catch(libhttppp::HTTPException &e){
      std::cerr << e.what() << std::endl;
+12 −9
Original line number Diff line number Diff line
@@ -53,10 +53,13 @@ void sendResponse(libhttppp::HttpRequest *curreq) {
     if(curreq->isMobile())
         condat << "<span>You are using a mobile Browser!</span><br>";
     for(libhttppp::HttpHeader::HeaderData *preq = curreq->getfirstHeaderData(); preq; preq=curreq->nextHeaderData(preq)){
       condat  << curreq->getKey(preq) 
               << ": "
               << curreq->getValue(preq)
               << "<br/>";
       condat  << preq->getkey()
               << ": ";
       for(libhttppp::HttpHeader::HeaderData::Values *cval=preq->getfirstValue(); cval; cval=cval->nextvalue()){
          condat  << cval->getvalue();
       }

       condat << "<br/>";
     }
     condat  << "</body></html>";
     libhtmlpp::HtmlString html;
@@ -86,16 +89,16 @@ class HttpConD : public libhttppp::HttpD {
public:
  HttpConD(int argc, char** argv) : HttpD(argc,argv){
    libhttppp::HTTPException httpexception;
    try {
      Controller controller(getServerSocket());
      controller.runEventloop();
    }catch(libhttppp::HTTPException &e){
      std::cout << e.what() << std::endl;
    }
  };
private:
};

int main(int argc, char** argv){
  try {
    HttpConD(argc,argv);
  }catch(libhttppp::HTTPException &e){
    std::cout << e.what() << std::endl;
  }
}
+9 −4
Original line number Diff line number Diff line
@@ -50,12 +50,17 @@ void sendResponse(libhttppp::HttpRequest *curreq) {
             << "    <style></style>"
             << "  </head>"
             << "<body>";

     for(libhttppp::HttpHeader::HeaderData *preq = curreq->getfirstHeaderData(); preq; preq=curreq->nextHeaderData(preq)){
       condat  << curreq->getKey(preq) 
               << ": "
               << curreq->getValue(preq)
               << "<br/>";
       condat  << preq->getkey()
               << ": ";
       for(libhttppp::HttpHeader::HeaderData::Values *cval=preq->getfirstValue(); cval; cval=cval->nextvalue()){
          condat  << cval->getvalue();
       }

       condat << "<br/>";
     }

     condat  << "</body></html>";
     libhtmlpp::HtmlString html;
     libhtmlpp::print(condat.parse(),html);
+396 −235

File changed.

Preview size limit exceeded, changes collapsed.

+47 −23
Original line number Diff line number Diff line
@@ -47,37 +47,61 @@ namespace libhttppp {
  public:
    class HeaderData{
    public:
      HeaderData &operator<<(const char *value);
      HeaderData &operator<<(size_t value);
      HeaderData &operator<<(int value);
    class Values{
      public:
        Values &operator=(const char *val);
        Values &operator=(size_t val);
        Values &operator=(int val);

        Values &operator<<(const char *value);
        Values &operator<<(size_t value);
        Values &operator<<(int value);

        std::string &getvalue();
        int          getIntvalue();
        size_t       getSizetValue();

        Values      *nextvalue();
        Values(const char *value);
        ~Values();
      private:
        std::string  _value;
        Values      *_nextvalue;
        friend class HeaderData;
      };

      Values *getfirstValue();
      Values &at(int pos);
      Values &operator[](int pos);

      void push_back(std::string val);
      void push_back(const char* val);
      void push_back(size_t val);
      void push_back(int val);

      HeaderData &operator=(const char *value);
      HeaderData &operator=(size_t value);
      HeaderData &operator=(int value);
      bool empty();

      void erase(int pos);

      void clear();

      const char *getkey();

    private:
      HeaderData(const char *key);
      ~HeaderData();
      std::string  _Key;
      std::string  _Value;
      Values      *_firstValue;
      Values      *_lastValue;
      HeaderData  *_nextHeaderData;
      friend class HttpHeader;
    };
    
    
    HeaderData         *getfirstHeaderData();
    HeaderData         *nextHeaderData(HeaderData *pos);
    const char *getKey(HeaderData *pos);
    const char *getValue(HeaderData *pos);
    
    HeaderData *setData(const char *key);
    
    HeaderData *getData(const char *key);
    HeaderData         *getHeaderData(const char *key);
    HeaderData         *setHeaderData(const char *key);

    const char *getData(HeaderData *pos);
    size_t      getDataSizet(HeaderData *pos);
    int         getDataInt(HeaderData *pos);
    void             deldata(const char *key);
    void             deldata(HeaderData*pos);
    
Loading