Overview
Features
Download
Documentation
Community
Add-Ons & Services

HTTPStreamFactory and authentication

General discussion regarding the development of POCO for contributors.

HTTPStreamFactory and authentication

Postby callcc » 06 Sep 2010, 09:59

It seems OK to try to authenticate if user provides the authentication information in URI. The following patch (with the spaces messed up by the CODE) adds authentication (only Basic scheme) handling to the 'open' method of HTTPStreamFactory:
Code: Select all
Index: Net/include/Poco/Net/HTTPStreamFactory.h
===================================================================
--- Net/include/Poco/Net/HTTPStreamFactory.h   (revision 1403)
+++ Net/include/Poco/Net/HTTPStreamFactory.h   (working copy)
@@ -94,7 +94,10 @@
    static void unregisterFactory();
       /// Unregisters the HTTPStreamFactory with the
       /// default URIStreamOpener instance.   
-      
+
+protected:
+   static bool splitUserInfo(const std::string& userInfo, std::string& username, std::string& password);
+
 private:
    enum
    {
Index: Net/src/HTTPStreamFactory.cpp
===================================================================
--- Net/src/HTTPStreamFactory.cpp   (revision 1403)
+++ Net/src/HTTPStreamFactory.cpp   (working copy)
@@ -39,6 +39,7 @@
 #include "Poco/Net/HTTPIOStream.h"
 #include "Poco/Net/HTTPRequest.h"
 #include "Poco/Net/HTTPResponse.h"
+#include "Poco/Net/HTTPBasicCredentials.h"
 #include "Poco/Net/NetException.h"
 #include "Poco/URI.h"
 #include "Poco/URIStreamOpener.h"
@@ -106,6 +107,10 @@
          std::string path = resolvedURI.getPathAndQuery();
          if (path.empty()) path = "/";
          HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
+         std::string username;
+         std::string password;
+         if (splitUserInfo(resolvedURI.getUserInfo(), username, password))
+            HTTPBasicCredentials(username, password).authenticate(req);
          pSession->sendRequest(req);
          HTTPResponse res;
          std::istream& rs = pSession->receiveResponse(res);
@@ -155,4 +160,21 @@
 }
 
 
+bool HTTPStreamFactory::splitUserInfo(const std::string& userInfo, std::string& username, std::string& password)
+{
+   if (userInfo.empty())
+      return false;
+   std::string::size_type pos = userInfo.find(':');
+   if (pos != std::string::npos)
+   {
+      if (userInfo.find(':', pos + 1) != std::string::npos)
+         throw InvalidArgumentException("Malformed user info", userInfo);
+      username.assign(userInfo, 0, pos);
+      password.assign(userInfo, pos + 1, std::string::npos);
+   }
+   else username = userInfo;
+   return true;
+}
+
+
 } } // namespace Poco::Net
callcc
 
Posts: 9
Joined: 12 Apr 2010, 12:14
Location: Russia

Return to Contributors

Who is online

Users browsing this forum: No registered users and 2 guests

cron