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

modernize

parent c4ee3359
Loading
Loading
Loading
Loading
+8 −8
Changes for examples/httpcookie.cpp: 8 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@
#include "http.h"
#include "httpd.h"

const std::string emtpystring="";

class CookieTest {
public:
@@ -67,22 +68,21 @@ public:
        _Curres.send(_Curreq,html.data(),html.size());
    };
    
    const char *getData(const char *key,libhttppp::HttpForm &curform){
    const std::string &getData(const std::string &key,libhttppp::HttpForm &curform){
        for(libhttppp::HttpForm::UrlcodedForm::Data *cururlform=curform.UrlFormData.getFormData() ; cururlform;
            cururlform=cururlform->nextData()){
            if(strcmp(key,cururlform->getKey())==0)
            if(cururlform->getKey()==key)
                return cururlform->getValue();
        }

        return nullptr;
        return emtpystring;
    }
    
    int getDataInt(const char *key,libhttppp::HttpForm &curform){
    int getDataInt(const std::string &key,libhttppp::HttpForm &curform){
        for(libhttppp::HttpForm::UrlcodedForm::Data *cururlform=curform.UrlFormData.getFormData(); cururlform;
            cururlform=cururlform->nextData()){
            if(strcmp(key,cururlform->getKey())==0){
            if(key==cururlform->getKey()){
                    char ktmp[255];
                    memcpy(ktmp,cururlform->getValue(),strlen(cururlform->getValue())+1);
                    memcpy(ktmp,cururlform->getValue().c_str(),cururlform->getValue().length());
                    return atoi(ktmp);
                }
        }
@@ -106,7 +106,7 @@ public:

        libhttppp::HttpForm curform;
        curform.parse(_Curreq);
        if(getData("key",curform)!=nullptr){
        if(!getData("key",curform).empty()){
            _Cookie.setcookie(_Curres,getData("key",curform),getData("value",curform),getData("comment",curform),getData("domain",curform),
                              getDataInt("max-age",curform),getData("path",curform),false,getData("version",curform));
        }
+2 −2
Changes for examples/httpform.cpp: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -78,9 +78,9 @@ private:
                    curdispo; curdispo=curdispo->nextContentDisposition()
                ){
                        condat << "Key: "
                               << curdispo->getKey()
                               << curdispo->getKey().data()
                               << " Value: "
                               << curdispo->getValue()
                               << curdispo->getValue().data()
                               << " ";
                }
                condat << "<br>";
+40 −40
Changes for src/http.cpp: 40 added lines, 40 removed lines.
Original line number Diff line number Diff line
@@ -1311,7 +1311,7 @@ bool libhttppp::HttpRequest::isMobile(){
  return false;
}

void libhttppp::HttpRequest::setRequestData(const char* data, size_t len){
void libhttppp::HttpRequest::setRequestData(const std::string &data, size_t len){
  _Request.clear();
  _Request.resize(len);
  _Request.insert(0,data);
@@ -1322,8 +1322,8 @@ void libhttppp::HttpRequest::setMaxUploadSize(size_t upsize){
}


void libhttppp::HttpRequest::setRequestVersion(const char* version){
  if(version)
void libhttppp::HttpRequest::setRequestVersion(const std::string& version){
  if(!version.empty())
    _RequestVersion=version;
  else
    _RequestVersion.clear();
@@ -1360,8 +1360,8 @@ void libhttppp::HttpRequest::setRequestType(int req){
   throw excep;
}

void libhttppp::HttpRequest::setRequestURL(const char* url){
  if(url)
void libhttppp::HttpRequest::setRequestURL(const std::string &url){
  if(!url.empty())
    _RequestURL=url;
  else
    _RequestURL.clear();
@@ -1567,7 +1567,7 @@ void libhttppp::HttpForm::_parseMultiSection(netplus::condata<char> &data,size_t
  MultipartForm::Data::Content *cdispo=nullptr;

  for(MultipartForm::Data::Content *curcontent=MultipartFormData._lastData->_firstContent; curcontent; curcontent=curcontent->nextContent()){
    if( strcmp(curcontent->getKey(),"content-disposition") ==0 ) {
    if( strcmp(curcontent->getKey().data(),"content-disposition") ==0 ) {
      cdispo=curcontent;
    }
  }
@@ -1733,41 +1733,41 @@ libhttppp::HttpForm::MultipartForm::Data::Content::Content(){
libhttppp::HttpForm::MultipartForm::Data::Content::~Content(){
}

void libhttppp::HttpForm::MultipartForm::Data::Content::setKey(const char* key){
void libhttppp::HttpForm::MultipartForm::Data::Content::setKey(const std::vector<char> & key){
  _Key.clear();
  std::copy(key,key+(strlen(key)+1),std::inserter<std::vector<char>>(_Key,_Key.begin()));
  std::copy(key.begin(),key.end(),std::inserter<std::vector<char>>(_Key,_Key.begin()));
}

void libhttppp::HttpForm::MultipartForm::Data::Content::setValue(const char* value){
void libhttppp::HttpForm::MultipartForm::Data::Content::setValue(const std::vector<char> &value){
  _Value.clear();
  std::copy(value,value+(strlen(value)+1),std::inserter<std::vector<char>>(_Value,_Value.begin()));
  std::copy(value.begin(),value.end(),std::inserter<std::vector<char>>(_Value,_Value.begin()));
}

const char * libhttppp::HttpForm::MultipartForm::Data::Content::getKey(){
  return _Key.data();
const std::vector<char> &libhttppp::HttpForm::MultipartForm::Data::Content::getKey() const{
  return _Key;
}

const char * libhttppp::HttpForm::MultipartForm::Data::Content::getValue(){
  return _Value.data();
const std::vector<char> &libhttppp::HttpForm::MultipartForm::Data::Content::getValue() const{
  return _Value;
}


const char *libhttppp::HttpForm::MultipartForm::Data::ContentDisposition::getKey(){
  return _Key.data();
const std::vector<char> &libhttppp::HttpForm::MultipartForm::Data::ContentDisposition::getKey() const{
  return _Key;
}

const char *libhttppp::HttpForm::MultipartForm::Data::ContentDisposition::getValue(){
  return _Value.data();
const std::vector<char> &libhttppp::HttpForm::MultipartForm::Data::ContentDisposition::getValue() const{
  return _Value;
}

void libhttppp::HttpForm::MultipartForm::Data::ContentDisposition::setKey(const char* key){
void libhttppp::HttpForm::MultipartForm::Data::ContentDisposition::setKey(const std::vector<char> &key){
  _Key.clear();
  std::copy(key,key+(strlen(key)+1),std::inserter<std::vector<char>>(_Key,_Key.begin()));
  std::copy(key.begin(),key.end(),std::inserter<std::vector<char>>(_Key,_Key.begin()));
}

void libhttppp::HttpForm::MultipartForm::Data::ContentDisposition::setValue(const char* value){
void libhttppp::HttpForm::MultipartForm::Data::ContentDisposition::setValue(const std::vector<char> & value){
  _Value.clear();
  std::copy(value,value+(strlen(value)+1),std::inserter<std::vector<char>>(_Value,_Value.begin()));
  std::copy(value.begin(),value.end(),std::inserter<std::vector<char>>(_Value,_Value.begin()));
}


@@ -1836,8 +1836,8 @@ void libhttppp::HttpForm::_parseUrlDecode(const netplus::condata<char> &data,siz
  }
}

void libhttppp::HttpForm::UrlcodedForm::Data::setKey(const char* key){
   if(!key){
void libhttppp::HttpForm::UrlcodedForm::Data::setKey(const std::string &key){
   if(key.empty()){
	HTTPException httpexception;
	httpexception[HTTPException::Error] << "no urldecoded key set";
	throw httpexception;
@@ -1845,20 +1845,20 @@ void libhttppp::HttpForm::UrlcodedForm::Data::setKey(const char* key){
   _Key=key;
}

void libhttppp::HttpForm::UrlcodedForm::Data::setValue(const char* value){
  if(!value){
void libhttppp::HttpForm::UrlcodedForm::Data::setValue(const std::string &value){
  if(value.empty()){
      _Value.clear();
      return;
  }
  _Value=value;
}

const char *libhttppp::HttpForm::UrlcodedForm::Data::getKey(){
  return _Key.c_str();
const std::string &libhttppp::HttpForm::UrlcodedForm::Data::getKey(){
  return _Key;
}

const char *libhttppp::HttpForm::UrlcodedForm::Data::getValue(){
  return _Value.c_str();
const std::string &libhttppp::HttpForm::UrlcodedForm::Data::getValue(){
  return _Value;
}

libhttppp::HttpForm::UrlcodedForm::Data  *libhttppp::HttpForm::UrlcodedForm::Data::nextData(){
@@ -1944,12 +1944,12 @@ libhttppp::HttpCookie::~HttpCookie(){


void libhttppp::HttpCookie::setcookie(libhttppp::HttpResponse &curresp,
                                      const char* key, const char* value,
                                      const char *comment,const char *domain,  
                                      int maxage, const char* path,
                                      bool secure,const char *version,const char *samesite,bool httponly){
                                      const std::string & key, const std::string &value,
                                      const std::string &comment,const std::string &domain,
                                      int maxage, const std::string & path,
                                      bool secure,const std::string &version,const std::string &samesite,bool httponly){
    HTTPException httpexception;
    if(!key || !value){
    if(key.empty()){
        httpexception[HTTPException::Note] << "no key or value set in cookie!";
        throw httpexception;
    }
@@ -1960,12 +1960,12 @@ void libhttppp::HttpCookie::setcookie(libhttppp::HttpResponse &curresp,
    buf+=value;
    dat->push_back(buf);

    if(comment){
    if(!comment.empty()){
        buf="comment=";
        buf+=comment;
        dat->push_back(buf);
    }
    if(domain){
    if(!domain.empty()){
        buf="domain=";
        buf+=domain;
        dat->push_back(buf);
@@ -1976,12 +1976,12 @@ void libhttppp::HttpCookie::setcookie(libhttppp::HttpResponse &curresp,
        ss << maxage;
        dat->push_back(ss.str());
    }
    if(path){
    if(!path.empty()){
        buf="path=";
        buf+=path;
        dat->push_back(buf);
    }
    if(samesite){
    if(!samesite.empty()){
        buf="sameSite=";
        buf+=samesite;
        dat->push_back(buf);
@@ -1994,7 +1994,7 @@ void libhttppp::HttpCookie::setcookie(libhttppp::HttpResponse &curresp,
        buf+=secure;
        dat->push_back(buf);
    }
    if(version){
    if(!version.empty()){
        buf="version=";
        buf+=version;
        dat->push_back(buf);
+19 −19
Changes for src/http.h: 19 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -232,10 +232,10 @@ namespace libhttppp {

    /*Client methods*/
    void              setRequestType(int req);
    void              setRequestURL(const char *url);
    void              setRequestVersion(const char *version);
    void              setRequestURL(const std::string &url);
    void              setRequestVersion(const std::string &version);
    /*only for post Reuquesttype*/
    void              setRequestData(const char *data,size_t len);
    void              setRequestData(const std::string &data,size_t len);
    void              setMaxUploadSize(size_t upsize);

    void              send(netplus::socket &dest);
@@ -258,11 +258,11 @@ namespace libhttppp {
        public:
        class Content {
        public:
          const char *getKey();
          const char *getValue();
          const std::vector<char> &getKey() const;
          const std::vector<char> &getValue() const;

          void setKey(const char *key);
          void setValue(const char *value);
          void setKey(const std::vector<char> &key);
          void setValue(const std::vector<char> &value);

          Content();
          ~Content();
@@ -279,11 +279,11 @@ namespace libhttppp {

        class ContentDisposition{
        public:
          const char *getKey();
          const char *getValue();
          const std::vector<char> &getKey() const;
          const std::vector<char> &getValue() const;

          void setKey(const char *key);
          void setValue(const char *value);
          void setKey(const std::vector<char> &key);
          void setValue(const std::vector<char> &value);

          ContentDisposition();
          ~ContentDisposition();
@@ -339,11 +339,11 @@ namespace libhttppp {
          Data(const Data &fdat);
          Data(const char *key,const char *value);
          ~Data();
          const char   *getKey();
          const char   *getValue();
          const std::string &getKey();
          const std::string &getValue();
      
          void          setKey(const char *key);
          void          setValue(const char *value);
          void          setKey(const std::string &key);
          void          setValue(const std::string &value);
          Data         *nextData();
        private:
          std::string        _Key;
@@ -409,10 +409,10 @@ namespace libhttppp {
    ~HttpCookie();
    void parse(libhttppp::HttpRequest& curreq);
    void setcookie(libhttppp::HttpResponse& curresp,
                   const char *key,const char *value,
                   const char *comment=nullptr,const char *domain=nullptr, 
                   int maxage=-1,const char *path=nullptr,
                   bool secure=false,const char *version="1",const char *samesite=nullptr,bool httponly=false);
                   const std::string &key,const std::string &value,
                   const std::string &comment=nullptr,const std::string &domain=nullptr,
                   int maxage=-1,const std::string &path=nullptr,
                   bool secure=false,const std::string &version="1",const std::string &samesite=nullptr,bool httponly=false);
    CookieData    *getfirstCookieData();
    CookieData    *getlastCookieData();
    CookieData    *addCookieData();