]> git.tdb.fi Git - libs/net.git/blobdiff - source/request.cpp
Add functions for parsing and building URIs and query strings
[libs/net.git] / source / request.cpp
index bcf36c991f727f5c539a6431304d68691dd5be92..6bad3700681fefdffd40178a88e44cb146442e0b 100644 (file)
@@ -9,6 +9,7 @@ Distributed under the LGPL
 #include <msp/strings/regex.h>
 #include <msp/strings/utils.h>
 #include "request.h"
+#include "utils.h"
 
 using namespace std;
 
@@ -45,23 +46,25 @@ Request Request::parse(const string &str)
        return result;
 }
 
-Request Request::from_url(const string &url)
+Request Request::from_url(const string &str)
 {
-       if(RegMatch match=Regex("^http://([a-zA-Z0-9.-]+(:[0-9]+)?)(/[^ #]*)?$").match(url))
+       Url url=parse_url(str);
+       if(url.scheme!="http")
+               throw InvalidParameterValue("Only http scheme is supported");
+       string path=url.path;
+       if(path.empty())
+               path="/";
+       if(!url.query.empty())
        {
-               string host=match[1].str;
-               string path=match[3].str;
-               if(path.empty())
-                       path="/";
+               path+='?';
+               path+=url.query;
+       }
 
-               Request result("GET", path);
-               result.set_header("Host", host);
-               result.set_header("Connection", "close");
+       Request result("GET", path);
+       result.set_header("Host", url.host);
+       result.set_header("Connection", "close");
 
-               return result;
-       }
-       else
-               throw InvalidParameterValue("Invalid URL");
+       return result;
 }
 
 } // namespace Http