Overview
Features
Download
Documentation
Community
Add-Ons & Services

[RESOLVED] HTTP Server get remote address, and sessions

Please post support and help requests here.

[RESOLVED] HTTP Server get remote address, and sessions

Postby budric » 03 Jul 2009, 16:58

Hi,
I have 2 questions about implementing an HTTP Server.
1. How can I get the name of the client making the request in HTTPRequestHandler?
2. Does POCO support any session tracking? I.E. generate a session ID, set it inside a cookie or URL rewriting to track a user. What set of classes/functions/examples can I look at?
Last edited by budric on 03 Jul 2009, 20:24, edited 1 time in total.
budric
 
Posts: 24
Joined: 13 May 2009, 17:14

Re: HTTP Server get remote address, and sessions

Postby aderouineau » 03 Jul 2009, 17:49

Unfortunately examples, and especially the documentations, are scarce.

There is no predefined function for reading the user agent string (perhaps it would be nice to add it). Getting it shouldn't be complicated, though. Assuming request is the instance of HTTPRequest (or HTTPServerRequest), do: request.get("User-Agent");

const std::string & Poco::Net::HTTPRequest::get(const std::string & name)
Returns the value of the first name-value pair with the given name.
Throws a NotFoundException if the name-value pair does not exist.

const std::string & Poco::Net::HTTPRequest::get(const std::string & name, const std::string & defaultValue)
Returns the value of the first name-value pair with the given name. If no value with the given name has been found, the defaultValue is returned.

-----

To generate a session ID, your program has to do that. You could use one of POCO's generators. A semi-random one you might like is Poco::UUIDGenerator.
Then you generate a cookie with Poco::Net::Cookie, and then you add that cookie to the response using Poco::Net::HTTPResponse::addCookie(...).
aderouineau
 
Posts: 163
Joined: 18 May 2009, 17:38

Re: HTTP Server get remote address, and sessions

Postby budric » 03 Jul 2009, 17:56

For the random ID generator I was going to use the requestor's address and 4 random numbers put into MD5 hash. I believe that's similar to the way JSESSION is generated. Hence the first question.
budric
 
Posts: 24
Joined: 13 May 2009, 17:14

Re: HTTP Server get remote address, and sessions

Postby aderouineau » 03 Jul 2009, 19:46

budric wrote:For the random ID generator I was going to use the requestor's address [...]. Hence the first question.

It seems you chose the wrong words in your original question. If what you want is the client address and you use POCO's HTTP Server, then you can do, assuming request is an instance of HTTPServerRequest: request.clientAddress() which returns a Poco::Net::SocketAddress.

I recommend you check the API reference. With some scavenging skills you should find the answers to most of your current and future questions.
http://pocoproject.org/docs/


budric wrote:and 4 random numbers put into MD5 hash. I believe that's similar to the way JSESSION is generated.


You are not obliged to use the Java way of generating session id's. You can use a UUID and MD5 it, as so:

Code: Select all
Poco::DigestOutputStream oscrypt = Poco::DigestOutputStream(new Poco::MD5Engine());
oscrypt << Poco::UUIDGenerator::createRandom().toString();
std::string sessionId = oscrypt.digestToHex();


----------------------------------------------------------------------------------------------------

If you consider that this issue has been resolved, please add [RESOLVED] at the beginning of the topic title.
aderouineau
 
Posts: 163
Joined: 18 May 2009, 17:38

Re: HTTP Server get remote address, and sessions

Postby budric » 03 Jul 2009, 20:24

Thanks for your help.
budric
 
Posts: 24
Joined: 13 May 2009, 17:14

Re: [RESOLVED] HTTP Server get remote address, and sessions

Postby aderouineau » 04 Jul 2009, 00:50

Be careful with the code I wrote. I didn't check it, so you can kinda consider it like pseudo-code; and using new for the parameter creates a memory leak because the allocated MD5Engine is never released.
aderouineau
 
Posts: 163
Joined: 18 May 2009, 17:38


Return to Support

Who is online

Users browsing this forum: No registered users and 4 guests

cron