1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2005-2016. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%% 20%% 21-module(httpd_cgi). 22 23-export([parse_headers/1, handle_headers/1]). 24 25-include_lib("inets/src/inets_app/inets_internal.hrl"). 26 27 28%%%========================================================================= 29%%% Internal application API 30%%%========================================================================= 31 32%%-------------------------------------------------------------------------- 33%% parse_headers([Bin, Data, Header, Headers]) -> {RevHeaders, Body} | 34%% {Module, Function, Args} 35%% Bin = Data = binary() 36%% Header = string() - Accumulator should be [] in first call 37%% Headers = [Header] - Accumulator should be [] in first call 38%% Body = string() 39%% RevHeaders = string() - Note CGI-headers not HTTP-headers 40%% 41%% Description: Parses "<<Bin/binary, Data/binary>>" returned from the 42%% CGI-script until it findes the end of the CGI-headers (at least one 43%% CGI-HeaderField must be supplied) then it returns the CGI-headers 44%% and maybe some body data. If {Module, Function, Args} is 45%% returned it means that more data needs to be collected from the 46%% cgi-script as the end of the headers was not yet found. When more 47%% data has been collected call Module:Function([NewData | Args]). 48%% 49%% NOTE: The headers are backwards and should 50%% be so, devide_and_reverse_headers will reverse them back after 51%% taking advantage of the fact that they where backwards. 52%%-------------------------------------------------------------------------- 53parse_headers([Data, Bin, Header, Headers]) -> 54 parse_headers(<<Bin/binary, Data/binary>>, Header, Headers). 55 56%%-------------------------------------------------------------------------- 57%% handle_headers(CGIHeaders) -> {ok, HTTPHeaders, StatusCode} | 58%% {proceed, AbsPath} 59%% CGIHeaders = [string()] 60%% HTTPHeaders = [{HeaderField, HeaderValue}] 61%% HeaderField = string() 62%% HeaderValue = string() 63%% StatusCode = integer() 64%% 65%% Description: Interprets CGI headers and creates HTTP headers and a 66%% appropriate HTTP status code. Note if a CGI location header is present 67%% the return value will be {proceed, AbsPath} 68%%-------------------------------------------------------------------------- 69handle_headers(CGIHeaders) -> 70 handle_headers(CGIHeaders, [], {200, "ok"}). 71 72%%%======================================================================== 73%%% Internal functions 74%%%======================================================================== 75parse_headers(<<>>, Header, Headers) -> 76 {?MODULE, parse_headers, [<<>>, Header, Headers]}; 77parse_headers(<<?CR,?LF>>, Header, Headers) -> 78 {?MODULE, parse_headers, [<<?CR,?LF>>, Header, Headers]}; 79parse_headers(<<?LF>>, Header, Headers) -> 80 {?MODULE, parse_headers, [<<?LF>>, Header, Headers]}; 81parse_headers(<<?CR, ?LF, ?CR, ?LF, Rest/binary>>, Header, Headers) -> 82 {ok, {[lists:reverse([?LF, ?CR | Header]) | Headers], Rest}}; 83parse_headers(<<?LF, ?LF, Rest/binary>>, Header, Headers) -> 84 {ok, {[lists:reverse([?LF | Header]) | Headers], Rest}}; 85parse_headers(<<?CR, ?LF, Rest/binary>>, Header, Headers) -> 86 parse_headers(Rest, [], [lists:reverse([?LF, ?CR | Header]) | Headers]); 87parse_headers(<<?LF, Rest/binary>>, Header, Headers) -> 88 parse_headers(Rest, [], [lists:reverse([?LF | Header]) | Headers]); 89parse_headers(<<Octet, Rest/binary>>, Header, Headers) -> 90 parse_headers(Rest, [Octet | Header], Headers). 91 92handle_headers([], HTTPHeaders, Status) -> 93 {ok, HTTPHeaders, Status}; 94 95handle_headers([CGIHeader | CGIHeaders], HTTPHeaders, Status) -> 96 97 {FieldName, FieldValue} = httpd_response:split_header(CGIHeader, []), 98 99 case FieldName of 100 "content-type" -> 101 handle_headers(CGIHeaders, 102 [{FieldName, FieldValue} | HTTPHeaders], 103 Status); 104 "location" -> 105 case http_request:is_absolut_uri(FieldValue) of 106 true -> 107 handle_headers(CGIHeaders, 108 [{FieldName, FieldValue} | 109 HTTPHeaders], {302, "Redirect"}); 110 false -> 111 {proceed, FieldValue} 112 end; 113 "status" -> 114 CodePhrase = 115 case httpd_util:split(FieldValue," ",2) of 116 {ok,[Code, Phrase]} -> 117 {list_to_integer(Code), Phrase}; 118 _ -> 119 {200, "OK"} 120 end, 121 handle_headers(CGIHeaders, HTTPHeaders, CodePhrase); 122 _ -> %% Extension headers 123 handle_headers(CGIHeaders, 124 [{FieldName, FieldValue} | HTTPHeaders], Status) 125 end. 126 127