The query part of the url

A url can have an optional query part. This part is passed in the A#arg.querydata which is passed as an argument to the out/1 function.

We show how to work with the query part of the url through an example, if we have a URL on the form of http://www.example.org/man.yaws?page=cat a key/value pair is passed to the page. In the above example, we have key=page and its value "cat". The code in the page man.yaws, will read these key/value pairs in the A#arg.querydata and display the man page.

Assuming a predifined CSS class called box, defined as:

      div.box { border: solid; border-width: thin; width: 90%;
      background: rgb(211, 211, 211)  }
    

The following code:



result(Status, Msg) ->
    [{status, Status},
     {ehtml,
      {html,[],
       {'div',[{class,"man"}],
        {pre,[],Msg}}}}].

out(A) ->
    try queryvar(A,"page") of
        {ok, Page} ->
            %% only allow regular chars in Page
            case lists:all(fun(C) ->
                                   if
                                       $a =< C, C =< $z ->
                                           true;
                                       $A =< C, C =< $Z ->
                                           true;
                                       C == $_ ->
                                           true;
                                       C == $. ->
                                           true;
                                       true ->
                                           false
                                   end
                           end, Page) of
                true ->
                    L = os:cmd("env LC_ALL=en_US.ISO-8859-1 man  " ++ Page ++ "  | col -b -p -x"),
                    result(200, L);
                false ->
                    result(400, "illegal character detected in query arg")
            end;
        undefined ->
            result(400, "no man page found in query arg")
    catch
        _:_ ->
            result(400, "illegal character detected in query arg")
    end.


will display a man page if invoked with a proper key/value pair in the query part of the URL.

This fairly convenient way of getting at the query (or POST) is equivalent of the code:

    P = yaws_api:parse_query(A),
    L = case lists:keysearch(page, 1, P) of
              {value, {page, Page}} ->
                   .....

The querypart of the URL is part as field in the Arg structure. The function parse_query/1 parses the raw data into a key/value list.

The queryvar(ARG,Key) function returns the value of the variable if it is found in the query part of the request. If the variable is not found or if the variable is unset, the queryvar(ARG,Key) function returns undefined.

Valid XHTML 1.0!