@@ -11,7 +11,7 @@ if Code.ensure_compiled(Mint.HTTP) == {:module, Mint.HTTP} do
11
11
[__Documentation__](https://hexdocs.pm/mint/Mint.HTTP.html)
12
12
"""
13
13
14
- alias Mint.HTTP
14
+ alias Mint.HTTP1 , as: Mint
15
15
16
16
alias Arangox . {
17
17
Client ,
@@ -41,7 +41,7 @@ if Code.ensure_compiled(Mint.HTTP) == {:module, Mint.HTTP} do
41
41
42
42
with (
43
43
{ :ok , conn } <- open ( addr , ssl? , options ) ,
44
- true <- HTTP . open? ( conn )
44
+ true <- Mint . open? ( conn )
45
45
) do
46
46
{ :ok , conn }
47
47
else
@@ -62,62 +62,81 @@ if Code.ensure_compiled(Mint.HTTP) == {:module, Mint.HTTP} do
62
62
defp open ( { :tcp , host , port } , ssl? , options ) do
63
63
scheme = if ssl? , do: :https , else: :http
64
64
65
- HTTP . connect ( scheme , host , port , options )
65
+ Mint . connect ( scheme , host , port , options )
66
66
end
67
67
68
68
@ impl true
69
- def request ( % Request { } = request , % Connection { } = state ) do
69
+ def request (
70
+ % Request { method: method , path: path , headers: headers , body: body } ,
71
+ % Connection { socket: socket } = state
72
+ ) do
70
73
{ :ok , conn , ref } =
71
- HTTP . request (
72
- state . socket ,
73
- request . method |> Atom . to_string ( ) |> String . upcase ( ) ,
74
- request . path ,
75
- Enum . into ( request . headers , [ ] , fn { k , v } -> { k , v } end ) ,
76
- request . body
74
+ Mint . request (
75
+ socket ,
76
+ method
77
+ |> to_string ( )
78
+ |> String . upcase ( ) ,
79
+ path ,
80
+ Enum . into ( headers , [ ] ) ,
81
+ body
77
82
)
78
83
79
- { new_conn , result } =
80
- case HTTP . recv ( conn , 0 , :infinity ) do
81
- { :ok , new_conn , stream } ->
82
- { new_conn , stream }
83
-
84
- { :error , new_conn , exception , _stream } ->
85
- { new_conn , exception }
86
- end
87
-
88
- new_state = % { state | socket: new_conn }
89
-
90
- if alive? ( new_state ) do
91
- case result do
92
- [
93
- { :status , ^ ref , status } ,
94
- { :headers , ^ ref , headers } ,
95
- { :done , ^ ref }
96
- ] ->
97
- { :ok , % Response { status: status , headers: Map . new ( headers ) } , new_state }
98
-
99
- [
100
- { :status , ^ ref , status } ,
101
- { :headers , ^ ref , headers } ,
102
- { :data , ^ ref , body } ,
103
- { :done , ^ ref }
104
- ] ->
105
- { :ok , % Response { status: status , headers: Map . new ( headers ) , body: body } , new_state }
106
-
107
- % _ { } = exception ->
108
- { :error , exception , new_state }
109
- end
110
- else
111
- { :error , :noproc , new_state }
84
+ case do_recv ( conn , ref ) do
85
+ { :ok , new_conn , buffer } ->
86
+ do_response ( ref , buffer , % { state | socket: new_conn } )
87
+
88
+ { :error , % _ { state: :closed } = new_conn , _ , ^ ref } ->
89
+ { :error , :noproc , % { state | socket: new_conn } }
90
+
91
+ { :error , new_conn , exception , ^ ref } ->
92
+ { :error , exception , % { state | socket: new_conn } }
93
+ end
94
+ end
95
+
96
+ def do_recv ( conn , ref , buffer \\ [ ] ) do
97
+ case Mint . recv ( conn , 0 , :infinity ) do
98
+ { :ok , new_conn , next_buffer } ->
99
+ if { :done , ref } in next_buffer do
100
+ { :ok , new_conn , buffer ++ next_buffer }
101
+ else
102
+ do_recv ( new_conn , ref , buffer ++ next_buffer )
103
+ end
104
+
105
+ { :error , _ , _ , ^ ref } = error ->
106
+ error
107
+ end
108
+ end
109
+
110
+ def do_response ( ref , buffer , state ) do
111
+ case buffer do
112
+ [ { :status , ^ ref , status } , { :headers , ^ ref , headers } , { :done , ^ ref } ] ->
113
+ { :ok , % Response { status: status , headers: Map . new ( headers ) } , state }
114
+
115
+ [ { :status , ^ ref , status } , { :headers , ^ ref , headers } , { :data , ^ ref , body } , { :done , ^ ref } ] ->
116
+ { :ok , % Response { status: status , headers: Map . new ( headers ) , body: body } , state }
117
+
118
+ [ { :status , ^ ref , status } , { :headers , ^ ref , headers } | rest_buffer ] ->
119
+ body =
120
+ for kv <- rest_buffer , into: "" do
121
+ case kv do
122
+ { :data , ^ ref , data } ->
123
+ data
124
+
125
+ { :done , ^ ref } ->
126
+ ""
127
+ end
128
+ end
129
+
130
+ { :ok , % Response { status: status , headers: Map . new ( headers ) , body: body } , state }
112
131
end
113
132
end
114
133
115
134
@ impl true
116
- def alive? ( % Connection { socket: conn } ) , do: HTTP . open? ( conn )
135
+ def alive? ( % Connection { socket: conn } ) , do: Mint . open? ( conn )
117
136
118
137
@ impl true
119
138
def close ( % Connection { socket: conn } ) do
120
- HTTP . close ( conn )
139
+ Mint . close ( conn )
121
140
122
141
:ok
123
142
end
0 commit comments