]> git.tdb.fi Git - libs/net.git/blobdiff - source/request.cpp
Initial revision
[libs/net.git] / source / request.cpp
diff --git a/source/request.cpp b/source/request.cpp
new file mode 100644 (file)
index 0000000..e63dc66
--- /dev/null
@@ -0,0 +1,54 @@
+/* $Id$
+
+This file is part of libmsphttp
+Copyright © 2008  Mikkosoft Productions, Mikko Rasa
+Distributed under the LGPL
+*/
+
+#include <msp/strings/formatter.h>
+#include <msp/strings/regex.h>
+#include <msp/strings/utils.h>
+#include "request.h"
+
+using namespace std;
+
+namespace Msp {
+namespace Http {
+
+Request::Request(const string &m, const string &p):
+       method(m),
+       path(p)
+{ }
+
+string Request::str() const
+{
+       string result=format("%s %s %s\r\n", method, path, version_str(http_version));
+       result+=str_common();
+
+       return result;
+}
+
+Request Request::from_url(const string &url)
+{
+       if(RegMatch match=Regex("^http://([a-zA-Z0-9.-]+)(:([0-9]+))?(/[^ #]*)?$").match(url))
+       {
+               string host=match[1].str;
+               string port="80";
+               if(match[3])
+                       port=match[3].str;
+               string path=match[4].str;
+               if(path.empty())
+                       path="/";
+
+               Request result("GET", path);
+               result.set_header("host", host);
+               result.set_header("x-port", port);
+
+               return result;
+       }
+       else
+               throw InvalidParameterValue("Invalid URL");
+}
+
+} // namespace Http
+} // namespace Msp