PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mochiweb_response.erl

http://github.com/basho/mochiweb
Erlang | 90 lines | 37 code | 13 blank | 40 comment | 0 complexity | cab99cbe333c869487e87a2ba16136ae MD5 | raw file
Possible License(s): MIT
  1. %% @author Bob Ippolito <bob@mochimedia.com>
  2. %% @copyright 2007 Mochi Media, Inc.
  3. %%
  4. %% Permission is hereby granted, free of charge, to any person obtaining a
  5. %% copy of this software and associated documentation files (the "Software"),
  6. %% to deal in the Software without restriction, including without limitation
  7. %% the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. %% and/or sell copies of the Software, and to permit persons to whom the
  9. %% Software is furnished to do so, subject to the following conditions:
  10. %%
  11. %% The above copyright notice and this permission notice shall be included in
  12. %% all copies or substantial portions of the Software.
  13. %%
  14. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. %% THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. %% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. %% DEALINGS IN THE SOFTWARE.
  21. %% @doc Response abstraction.
  22. -module(mochiweb_response).
  23. -author('bob@mochimedia.com').
  24. -define(QUIP, "Any of you quaids got a smint?").
  25. -export([new/3, get_header_value/2, get/2, dump/1]).
  26. -export([send/2, write_chunk/2]).
  27. %% @type response() = {atom(), [Request, Code, Headers]}
  28. %% @spec new(Request, Code, Headers) -> response()
  29. %% @doc Create a new mochiweb_response instance.
  30. new(Request, Code, Headers) ->
  31. {?MODULE, [Request, Code, Headers]}.
  32. %% @spec get_header_value(string() | atom() | binary(), response()) ->
  33. %% string() | undefined
  34. %% @doc Get the value of the given response header.
  35. get_header_value(K, {?MODULE, [_Request, _Code, Headers]}) ->
  36. mochiweb_headers:get_value(K, Headers).
  37. %% @spec get(request | code | headers, response()) -> term()
  38. %% @doc Return the internal representation of the given field.
  39. get(request, {?MODULE, [Request, _Code, _Headers]}) ->
  40. Request;
  41. get(code, {?MODULE, [_Request, Code, _Headers]}) ->
  42. Code;
  43. get(headers, {?MODULE, [_Request, _Code, Headers]}) ->
  44. Headers.
  45. %% @spec dump(response()) -> {mochiweb_request, [{atom(), term()}]}
  46. %% @doc Dump the internal representation to a "human readable" set of terms
  47. %% for debugging/inspection purposes.
  48. dump({?MODULE, [Request, Code, Headers]}) ->
  49. [{request, Request:dump()},
  50. {code, Code},
  51. {headers, mochiweb_headers:to_list(Headers)}].
  52. %% @spec send(iodata(), response()) -> ok
  53. %% @doc Send data over the socket if the method is not HEAD.
  54. send(Data, {?MODULE, [Request, _Code, _Headers]}) ->
  55. case Request:get(method) of
  56. 'HEAD' ->
  57. ok;
  58. _ ->
  59. Request:send(Data)
  60. end.
  61. %% @spec write_chunk(iodata(), response()) -> ok
  62. %% @doc Write a chunk of a HTTP chunked response. If Data is zero length,
  63. %% then the chunked response will be finished.
  64. write_chunk(Data, {?MODULE, [Request, _Code, _Headers]}=THIS) ->
  65. case Request:get(version, THIS) of
  66. Version when Version >= {1, 1} ->
  67. Length = iolist_size(Data),
  68. send([io_lib:format("~.16b\r\n", [Length]), Data, <<"\r\n">>], THIS);
  69. _ ->
  70. send(Data, THIS)
  71. end.
  72. %%
  73. %% Tests
  74. %%
  75. -ifdef(TEST).
  76. -include_lib("eunit/include/eunit.hrl").
  77. -endif.