#1 2014-12-07 19:28:19

alexsv
Member
Registered: 2014-12-07
Posts: 3

Adding ClientIP/Port and Cookie to HTTP API Server

Thank you for great product!

I'm trying to use Http API Sever separately of framework and found that there is no access to Client IP/Port and Cookie info.
I've added it for me but is it possible to add these properties to main source?

Suppose it will be helpful for sessions support when using HttpAPI server separately.

What was added:

  THttpServerRequest = class
  ..
   protected
   ...
    fClientIP: rawbytestring;
    fClientPort: integer;
    fCookie: rawbytestring;
  public
    property ClientIP: rawbytestring read fClientIP write fClientIP; 
    property ClientPort: integer read fClientPort write fClientPort; 
    property Cookie: rawbytestring read fCookie write fCookie;


procedure THttpApiServer.Execute;
...
after           Context.OutCustomHeaders := '';

          Context.fClientIP:=Format('%d.%d.%d.%d', [Req.Address.pRemoteAddress.sin_addr.S_bytes[0],
           Req.Address.pRemoteAddress.sin_addr.S_bytes[1], Req.Address.pRemoteAddress.sin_addr.S_bytes[2], Req.Address.pRemoteAddress.sin_addr.S_bytes[3]]);
          Context.fClientPort:=Req.Address.pRemoteAddress.sin_port;
          SetString(Context.fCookie, Req.Headers.KnownHeaders[reqCookie].pRawValue, Req.Headers.KnownHeaders[reqCookie].RawValueLength);

Offline

#2 2014-12-08 08:33:35

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,236
Website

Re: Adding ClientIP/Port and Cookie to HTTP API Server

As we already stated several times, we handle the cookies/ip process at TSQLRest level.
So you could use code like you wrote to have it seperated at THttpApi instead.

But a few remarks:
- Your IP computation won't work as expected with IPv6, and in fact, there is already a 'RemoteIP: 127.0.0.1' entry in the generated HTTP headers, supporting IPv6.
- BTW you are mixing Ansi and Unicode strings by using format() and RawByteString
- I suspect you should better use getter functions instead of modifying the Execute method.

Offline

#3 2014-12-21 22:30:51

alexsv
Member
Registered: 2014-12-07
Posts: 3

Re: Adding ClientIP/Port and Cookie to HTTP API Server

Thanks for remarks!

ab wrote:

As we already stated several times, we handle the cookies/ip process at TSQLRest level.
So you could use code like you wrote to have it seperated at THttpApi instead.

But how to access Req variable (: PHTTP_REQUEST which contains parsed headers) from other class? It seems to be THttpApiServer.Execute local variable and doesn't published anywhere

Offline

#4 2014-12-21 22:41:49

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,236
Website

Re: Adding ClientIP/Port and Cookie to HTTP API Server

As I wrote there is already a 'RemoteIP: 127.0.0.1' entry in the generated HTTP headers, supporting IPv6.

Just use the incoming headers.

Offline

Board footer

Powered by FluxBB