How can i access the other header of yaws arg structure

Håkan Stenholm <>
Thu Jul 21 22:45:36 CEST 2005


MEENA SELVAM wrote:

>In this the fourth element of other header itself
>looks like a record.. that means i should look for the
>record definition of other, or is it possible to
>pattern match and get the actual IP?
>
>[_,_,FourthElement|_] = (A#arg.headers)#headers.Other
>
The above will not work Other should be other - as Other is a variable 
rather than a atom which is required for a record field name - records 
are internaly stored as tuples and all acess calls are translated to 
(erlang:)element/2 calls, the use of atoms is presumably required to be 
able map record fieldnames to tuple positions at compile time.

See "Programming Examples" 
(http://erlang.se/doc/doc-5.4.8/doc/programming_examples/part_frame.html) 
in the web documentation for a more indeapth explanation about records 
as well as chapter 8 in the  "Erlang Reference Manual" 
(http://erlang.se/doc/doc-5.4.8/doc/reference_manual/part_frame.html) 
web page.

>[_,_,_,_,IP] = FourthElement
>
>
>Fourth Element of other header actually contains the
>IP address which I am interested to access.
>
>Will the above work?
>
I'm somewhat unsure what kind or records / data objects are stored in 
headers.other but you will probably want to do something like:

%% select appropriate misc.header 
[_,_,Element|_] = (A#arg.headers)#headers.other,

%% get the IP address from Element
[_,_,_,_,IP] = Element,

%% In your example you asume that the entry Element, from the misc. headers,
%% is a 5 element list containing a IP address as it's last element. 
%% A personal guess would be that Element is a record with a 'ip' field where 
%% the ip is probably stored as a list of integers (e.g. [127,123,100,5]) so 
%% IP would be retrived as:  

IP = Element#somerecord.ip %% calls element(#somerecord.ip, Element)

%% or

#somerecord{ip = IP} = Element  %% pattern match syntax that allows 
retrieval of
                                %% several record fields

%% this could be combined into

[_,_,#somerecord{ip = IP}|_] = (A#arg.headers)#headers.other,

%% if you prefer to nest the pattern matching code - I usually find this 
somewhat
%% cumbersome to read especially if there are several '='.
%% You could even use:

#headers{other = [_,_,#somerecord{ip = IP}|_]} = A#arg.headers,

%% or

#arg{header = #headers{other = [_,_,#somerecord{ip = IP}|_]}} = A,



>
>A= {arg,#Port<16338.534>,
>        {headers,undefined,
>                 "image/gif, image/x-xbitmap,
>image/jpeg, image/pjpeg, */*",
>                 "www.msn.com",
>                 undefined,
>                 undefined,
>                 undefined,
>                 undefined,
>                 undefined,
>                 undefined,
>                 undefined,
>                 "Mozilla/4.0 (compatible; MSIE 6.0;
>Windows NT 5.1)",
>                 undefined,
>                 [],
>                 undefined,
>                 undefined,
>                 undefined,
>                 undefined,
>                 undefined,
>                 undefined,
>                 undefined,
>                
>[{http_header,0,"X-Yaws-Had-Clicert",undefined,"false"},
>                 
>{http_header,49,'X-Forwarded-For',undefined,"11.10.1.107"},
>                                      undefined,
>                                      "false"},
>                                  {http_header,
>                                      49,
>                                     
>'X-Forwarded-For',
>                                      undefined,
>                                      "11.10.1.107"},
>                                  {http_header,
>                                      7,
>                                      'Via',
>                                      undefined,
>                                      "1.1 www.apr.com
>(Alteon iSD-SSL/5.5)"},
>                                  {http_header,
>                                      0,
>                                     
>"X-Forwarded-By",
>                                      undefined,
>                                      "47.80.18.95"},
>                                  {http_header,
>                                      10,
>                                     
>'Accept-Encoding',
>                                      undefined,
>                                      "gzip,
>deflate"},
>                                  {http_header,
>
>
>meena
>
>--- Håkan Stenholm <>
>wrote:
>
>
>>MEENA SELVAM wrote:
>>
>>
>>>Hi,
>>>
>>>I have a arg record A passed by the yaws webserver
>>>
>>to
>>
>>>the web application:
>>>
>>>
>>>How can i access the 3rd element of the other
>>>
>>header,
>>
>>>whereas I have the other header itself is available
>>>
>>as
>>
>>>(A#arg.headers)#headers.Other
>>> 
>>>
>>>
>>#headers.other appears to be a list, the third
>>element of a list can be accessed in two ways:
>>
>>%% using pattern matching
>>[_,_,ThirdElement|_] = List
>>
>>%% or
>> 
>>%% using nth/2 in the lists module
>>ThirdElement = lists:nth(3, List)
>>
>>
>>
>>>A= {arg,#Port<16338.534>,
>>>                       {headers,undefined,
>>>                                "image/gif,
>>>image/x-xbitmap, image/jpeg, image/
>>>pjpeg, */*",
>>>                                "www.msn.com",
>>>                                undefined,
>>>                                undefined,
>>>                                undefined,
>>>                                undefined,
>>>                                undefined,
>>>                                undefined,
>>>                                undefined,
>>>                                "Mozilla/4.0
>>>(compatible; MSIE 6.0; Windows NT
>>>5.1)",
>>>                                undefined,
>>>                                [],
>>>                                undefined,
>>>                                undefined,
>>>                                undefined,
>>>                                undefined,
>>>                                undefined,
>>>                                undefined,
>>>                                undefined,
>>>                                [{http_header,
>>>                                     0,
>>>                                    
>>>"X-Yaws-Had-Clicert",
>>>                                     undefined,
>>>                                     "false"},
>>>                                 {http_header,
>>>                                     49,
>>>                                    
>>>'X-Forwarded-For',
>>>                                     undefined,
>>>                                    
>>>
>>"11.10.1.107"},
>>
>>>                                 {http_header,
>>>                                     7,
>>>                                     'Via',
>>>                                     undefined,
>>>                                     "1.1
>>>
>>www.msn.com
>>
>>>(Alteon iSD-SSL/5.5)"},
>>>                                 {http_header,
>>>                                     0,
>>>                                    
>>>"X-Forwarded-By",
>>>                                     undefined,
>>>                                    
>>>
>>"47.80.18.95"},
>>
>>>                                 {http_header,
>>>                                     10,
>>>                                    
>>>'Accept-Encoding',
>>>                                     undefined,
>>>                                     "gzip,
>>>deflate"},
>>>                                 {http_header,
>>>                                     11,
>>>                                    
>>>'Accept-Language',
>>>                                     undefined,
>>>                                     "en-us"}]},
>>>                      
>>>{http_request,'GET',{abs_path,"/"},{1,1}},
>>>                       undefined,
>>>                       "/",
>>>                       [],
>>>                       undefined,
>>>                       .... and much more
>>>
>>>
>>>The records arg and header are defined as below:
>>>
>>>-record(arg, {
>>>         clisock,        %% the socket leading to
>>>
>>the
>>
>>>peer client
>>>         headers,        %% headers
>>>         req,            %% request
>>>        
>>>         .....      some more
>>>        }).
>>>
>>>-record(headers, {
>>>         connection,
>>>         accept,
>>>         host,
>>>         if_modified_since,
>>>         if_match,
>>>         if_none_match,
>>>         if_range,
>>>         if_unmodified_since,
>>>         range,
>>>         referer,
>>>         user_agent,
>>>         accept_ranges,
>>>         cookie = [],
>>>         keep_alive,
>>>         location,
>>>         content_length,
>>>         content_type,
>>>         content_encoding,
>>>         authorization,
>>>         transfer_encoding,
>>>         other = []   %% misc other headers
>>>  }).
>>>
>>>
>>>
>>>__________________________________________________
>>>Do You Yahoo!?
>>>Tired of spam?  Yahoo! Mail has the best spam
>>>
>>protection around 
>>
>>>http://mail.yahoo.com 
>>>
>>> 
>>>
>>>
>>
>
>
>
>		
>____________________________________________________
>Start your day with Yahoo! - make it your home page 
>http://www.yahoo.com/r/hs 
> 
>
>




More information about the erlang-questions mailing list