@@ -64,28 +64,60 @@ new(Id) ->
64
64
65
65
{ok , # state {path_params = Params }}.
66
66
67
- run ({get , {Host , Port , Path }}, KeyGen , _ValueGen , _State ) ->
67
+ run ({get_re , {Host , Port , Path }, Headers }, KeyGen , _ValueGen , _State ) ->
68
+ Path1 = re :replace (Path , " %%K" , KeyGen (), [global , {return , list }]),
69
+ run ({get , {Host , Port , Path1 }, Headers }, KeyGen , _ValueGen , _State );
68
70
69
- Path1 = re :replace (Path , " %%V" , KeyGen (), [global , {return , list }]),
71
+ run ({get , {Host , Port , Path }, Headers }, _KeyGen , _ValueGen , _State ) ->
72
+ PUrl = # url {host = Host , port = Port , path = Path },
70
73
71
- PUrl = # url {host = Host , port = Port , path = Path1 },
72
-
73
- case do_get (PUrl ) of
74
+ case do_get (PUrl , Headers ) of
74
75
{not_found , _Url } ->
75
76
{ok , 1 };
76
- {ok , _Url , _Headers } ->
77
+ {ok , _Url , _Header } ->
78
+ {ok , 1 };
79
+ {error , Reason } ->
80
+ {error , Reason , 1 }
81
+ end ;
82
+
83
+ run ({put_re , {Host , Port , Path , Data }, Headers }, KeyGen , ValueGen , _State ) ->
84
+ Path1 = re :replace (Path , " %%K" , KeyGen (), [global , {return , list }]),
85
+ Value = re :replace (Data , " %%V" , ValueGen (), [global , {return , list }]),
86
+ run ({put , {Host , Port , Path1 , Value }, Headers }, KeyGen , ValueGen , _State );
87
+
88
+ run ({put , {Host , Port , Path , Data }, Headers }, _KeyGen , _ValueGen , _State ) ->
89
+ PUrl = # url {host = Host , port = Port , path = Path },
90
+
91
+ case do_put (PUrl , Headers , Data ) of
92
+ ok ->
93
+ {ok , 1 };
94
+ {error , Reason } ->
95
+ {error , Reason , 1 }
96
+ end ;
97
+
98
+ run ({post_re , {Host , Port , Path , Data }, Headers }, KeyGen , ValueGen , _State ) ->
99
+ Path1 = re :replace (Path , " %%K" , KeyGen (), [global , {return , list }]),
100
+ Value = re :replace (Data , " %%V" , ValueGen (), [global , {return , list }]),
101
+ run ({post , {Host , Port , Path1 , Value }, Headers }, KeyGen , ValueGen , _State );
102
+
103
+ run ({post , {Host , Port , Path , Data }, Headers }, _KeyGen , _ValueGen , _State ) ->
104
+ PUrl = # url {host = Host , port = Port , path = Path },
105
+
106
+ case do_post (PUrl , Headers , Data ) of
107
+ ok ->
77
108
{ok , 1 };
78
109
{error , Reason } ->
79
110
{error , Reason , 1 }
80
111
end ;
81
- run ({put , {Host , Port , Path , Data }}, KeyGen , _ValueGen , _State ) ->
82
- Path1 = re :replace (Path , " %%V" , KeyGen (), [global , {return , list }]),
83
112
84
- PUrl = # url {host = Host , port = Port , path = Path1 },
113
+ run ({delete_re , {Host , Port , Path }, Headers }, KeyGen , _ValueGen , _State ) ->
114
+ Path1 = re :replace (Path , " %%K" , KeyGen (), [global , {return , list }]),
115
+ run ({delete , {Host , Port , Path1 }, Headers }, KeyGen , _ValueGen , _State );
85
116
86
- Value = re :replace (Data , " %%V" , KeyGen (), [global , {return , list }]),
117
+ run ({delete , {Host , Port , Path }, Headers }, _KeyGen , _ValueGen , _State ) ->
118
+ PUrl = # url {host = Host , port = Port , path = Path },
87
119
88
- case do_put (PUrl , [], Value ) of
120
+ case do_delete (PUrl , Headers ) of
89
121
ok ->
90
122
{ok , 1 };
91
123
{error , Reason } ->
@@ -96,21 +128,16 @@ run({put, {Host, Port, Path, Data}}, KeyGen, _ValueGen, _State) ->
96
128
% % Internal functions
97
129
% % ====================================================================
98
130
99
- do_get (Url ) ->
100
- do_get (Url , []).
101
-
102
- do_get (Url , Opts ) ->
103
- case send_request (Url , [], get , [], [{response_format , binary }]) of
104
- {ok , " 404" , _Headers , _Body } ->
131
+ do_get (Url , Headers ) ->
132
+ % %case send_request(Url, [], get, [], [{response_format, binary}]) of
133
+ case send_request (Url , Headers , get , [], [{response_format , binary }]) of
134
+ {ok , " 404" , _Header , _Body } ->
105
135
{not_found , Url };
106
- {ok , " 300" , Headers , _Body } ->
107
- {ok , Url , Headers };
108
- {ok , " 200" , Headers , Body } ->
109
- case proplists :get_bool (body_on_success , Opts ) of
110
- true -> {ok , Url , Headers , Body };
111
- false -> {ok , Url , Headers }
112
- end ;
113
- {ok , Code , _Headers , _Body } ->
136
+ {ok , " 300" , Header , _Body } ->
137
+ {ok , Url , Header };
138
+ {ok , " 200" , Header , _Body } ->
139
+ {ok , Url , Header };
140
+ {ok , Code , _Header , _Body } ->
114
141
{error , {http_error , Code }};
115
142
{error , Reason } ->
116
143
{error , Reason }
@@ -122,8 +149,14 @@ do_put(Url, Headers, ValueGen) ->
122
149
true ->
123
150
ValueGen
124
151
end ,
125
- case send_request (Url , Headers ++ [{ 'Content-Type' , 'application/octet-stream' }] ,
152
+ case send_request (Url , Headers ,
126
153
put , Val , [{response_format , binary }]) of
154
+ {ok , " 200" , _Header , _Body } ->
155
+ ok ;
156
+ {ok , " 201" , _Header , _Body } ->
157
+ ok ;
158
+ {ok , " 202" , _Header , _Body } ->
159
+ ok ;
127
160
{ok , " 204" , _Header , _Body } ->
128
161
ok ;
129
162
{ok , Code , _Header , _Body } ->
@@ -132,6 +165,48 @@ do_put(Url, Headers, ValueGen) ->
132
165
{error , Reason }
133
166
end .
134
167
168
+ do_post (Url , Headers , ValueGen ) ->
169
+ Val = if is_function (ValueGen ) ->
170
+ ValueGen ();
171
+ true ->
172
+ ValueGen
173
+ end ,
174
+ case send_request (Url , Headers ,
175
+ post , Val , [{response_format , binary }]) of
176
+ {ok , " 200" , _Header , _Body } ->
177
+ ok ;
178
+ {ok , " 201" , _Header , _Body } ->
179
+ ok ;
180
+ {ok , " 202" , _Header , _Body } ->
181
+ ok ;
182
+ {ok , " 204" , _Header , _Body } ->
183
+ ok ;
184
+ {ok , Code , _Header , _Body } ->
185
+ {error , {http_error , Code }};
186
+ {error , Reason } ->
187
+ {error , Reason }
188
+ end .
189
+
190
+ do_delete (Url , Headers ) ->
191
+ case send_request (Url , Headers , delete , [], []) of
192
+ {ok , " 200" , _Header , _Body } ->
193
+ ok ;
194
+ {ok , " 201" , _Header , _Body } ->
195
+ ok ;
196
+ {ok , " 202" , _Header , _Body } ->
197
+ ok ;
198
+ {ok , " 204" , _Header , _Body } ->
199
+ ok ;
200
+ {ok , " 404" , _Header , _Body } ->
201
+ ok ;
202
+ {ok , " 410" , _Header , _Body } ->
203
+ ok ;
204
+ {ok , Code , _Header , _Body } ->
205
+ {error , {http_error , Code }};
206
+ {error , Reason } ->
207
+ {error , Reason }
208
+ end .
209
+
135
210
connect (Url ) ->
136
211
case erlang :get ({ibrowse_pid , Url # url .host }) of
137
212
undefined ->
0 commit comments