PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lhttpc_sock.erl

https://bitbucket.org/etc/lhttpc/
Erlang | 161 lines | 52 code | 10 blank | 99 comment | 0 complexity | 74156e589c830fb67338d3a40ec619e4 MD5 | raw file
  1. %%% ----------------------------------------------------------------------------
  2. %%% Copyright (c) 2009, Erlang Training and Consulting Ltd.
  3. %%% All rights reserved.
  4. %%%
  5. %%% Redistribution and use in source and binary forms, with or without
  6. %%% modification, are permitted provided that the following conditions are met:
  7. %%% * Redistributions of source code must retain the above copyright
  8. %%% notice, this list of conditions and the following disclaimer.
  9. %%% * Redistributions in binary form must reproduce the above copyright
  10. %%% notice, this list of conditions and the following disclaimer in the
  11. %%% documentation and/or other materials provided with the distribution.
  12. %%% * Neither the name of Erlang Training and Consulting Ltd. nor the
  13. %%% names of its contributors may be used to endorse or promote products
  14. %%% derived from this software without specific prior written permission.
  15. %%%
  16. %%% THIS SOFTWARE IS PROVIDED BY Erlang Training and Consulting Ltd. ''AS IS''
  17. %%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. %%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. %%% ARE DISCLAIMED. IN NO EVENT SHALL Erlang Training and Consulting Ltd. BE
  20. %%% LIABLE SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  21. %%% BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. %%% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. %%% OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. %%% ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. %%% ----------------------------------------------------------------------------
  26. %%% @private
  27. %%% @author Oscar Hellstrรถm <oscar@erlang-consulting.com>
  28. %%% @doc
  29. %%% This module implements wrappers for socket operations.
  30. %%% Makes it possible to have the same interface to ssl and tcp sockets.
  31. %%% @end
  32. %%% @type boolean() = bool().
  33. -module(lhttpc_sock).
  34. -export([
  35. connect/5,
  36. recv/2,
  37. recv/3,
  38. send/3,
  39. controlling_process/3,
  40. setopts/3,
  41. close/2
  42. ]).
  43. -include("lhttpc_types.hrl").
  44. %% @spec (Host, Port, Options, Timeout, SslFlag) -> {ok, Socket} | {error, Reason}
  45. %% Host = string() | ip_address()
  46. %% Port = integer()
  47. %% Options = [{atom(), term()} | atom()]
  48. %% Timeout = infinity | integer()
  49. %% SslFlag = boolean()
  50. %% Socket = socket()
  51. %% Reason = atom()
  52. %% @doc
  53. %% Connects to `Host' and `Port'.
  54. %% Will use the `ssl' module if `SslFlag' is `true' and gen_tcp otherwise.
  55. %% `Options' are the normal `gen_tcp' or `ssl' Options.
  56. %% @end
  57. -spec connect(host(), integer(), socket_options(), timeout(), boolean()) ->
  58. {ok, socket()} | {error, atom()}.
  59. connect(Host, Port, Options, Timeout, true) ->
  60. ssl:connect(Host, Port, Options, Timeout);
  61. connect(Host, Port, Options, Timeout, false) ->
  62. gen_tcp:connect(Host, Port, Options, Timeout).
  63. %% @spec (Socket, SslFlag) -> {ok, Data} | {error, Reason}
  64. %% Socket = socket()
  65. %% Length = integer()
  66. %% SslFlag = boolean()
  67. %% Data = term()
  68. %% Reason = atom()
  69. %% @doc
  70. %% Reads available bytes from `Socket'.
  71. %% Will block untill data is available on the socket and return the first
  72. %% packet.
  73. %% @end
  74. -spec recv(socket(), boolean()) ->
  75. {ok, any()} | {error, atom()} | {error, {http_error, string()}}.
  76. recv(Socket, true) ->
  77. ssl:recv(Socket, 0);
  78. recv(Socket, false) ->
  79. gen_tcp:recv(Socket, 0).
  80. %% @spec (Socket, Length, SslFlag) -> {ok, Data} | {error, Reason}
  81. %% Socket = socket()
  82. %% Length = integer()
  83. %% SslFlag = boolean()
  84. %% Data = term()
  85. %% Reason = atom()
  86. %% @doc
  87. %% Receives `Length' bytes from `Socket'.
  88. %% Will block untill `Length' bytes is available.
  89. %% @end
  90. -spec recv(socket(), integer(), boolean()) -> {ok, any()} | {error, atom()}.
  91. recv(_, 0, _) ->
  92. {ok, <<>>};
  93. recv(Socket, Length, true) ->
  94. ssl:recv(Socket, Length);
  95. recv(Socket, Length, false) ->
  96. gen_tcp:recv(Socket, Length).
  97. %% @spec (Socket, Data, SslFlag) -> ok | {error, Reason}
  98. %% Socket = socket()
  99. %% Data = iolist()
  100. %% SslFlag = boolean()
  101. %% Reason = atom()
  102. %% @doc
  103. %% Sends data on a socket.
  104. %% Will use the `ssl' module if `SslFlag' is set to `true', otherwise the
  105. %% gen_tcp module.
  106. %% @end
  107. -spec send(socket(), iolist() | binary(), boolean()) -> ok | {error, atom()}.
  108. send(Socket, Request, true) ->
  109. ssl:send(Socket, Request);
  110. send(Socket, Request, false) ->
  111. gen_tcp:send(Socket, Request).
  112. %% @spec (Socket, Pid, SslFlag) -> ok | {error, Reason}
  113. %% Socket = socket()
  114. %% Pid = pid()
  115. %% SslFlag = boolean()
  116. %% Reason = atom()
  117. %% @doc
  118. %% Sets the controlling proces for the `Socket'.
  119. %% @end
  120. -spec controlling_process(socket(), pid(), boolean()) ->
  121. ok | {error, atom()}.
  122. controlling_process(Socket, Pid, true) ->
  123. ssl:controlling_process(Socket, Pid);
  124. controlling_process(Socket, Pid, false) ->
  125. gen_tcp:controlling_process(Socket, Pid).
  126. %% @spec (Socket, Options, SslFlag) -> ok | {error, Reason}
  127. %% Socket = socket()
  128. %% Options = [atom() | {atom(), term()}]
  129. %% SslFlag = boolean()
  130. %% Reason = atom()
  131. %% @doc
  132. %% Sets options for a socket. Look in `inet:setopts/2' for more info.
  133. %% @end
  134. -spec setopts(socket(), socket_options(), boolean()) ->
  135. ok | {error, atom()}.
  136. setopts(Socket, Options, true) ->
  137. ssl:setopts(Socket, Options);
  138. setopts(Socket, Options, false) ->
  139. inet:setopts(Socket, Options).
  140. %% @spec (Socket, SslFlag) -> ok | {error, Reason}
  141. %% Socket = socket()
  142. %% SslFlag = boolean()
  143. %% Reason = atom()
  144. %% @doc
  145. %% Closes a socket.
  146. %% @end
  147. -spec close(socket(), boolean()) -> ok | {error, atom()}.
  148. close(Socket, true) ->
  149. ssl:close(Socket);
  150. close(Socket, false) ->
  151. gen_tcp:close(Socket).