1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
%% ``Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% The Initial Developer of the Original Code is Mobile Arts AB
%% Portions created by Mobile Arts are Copyright 2002, Mobile Arts AB
%% All Rights Reserved.''
%%
%% Created : 18 Dec 2001 by Johan Blom <[email protected]>
%%
-module(httpc_manager).
-behaviour(gen_server).
-include("http.hrl").
-define(HMACALL, ?MODULE).
-define(HMANAME, ?MODULE).
%%--------------------------------------------------------------------
%% External exports
-export([start_link/0,start/0,
request/1,cancel_request/1,
next_request/2,
register_socket/3,
abort_session/3,close_session/2,close_session/3
]).
%% Debugging only
-export([status/0]).
%% gen_server callbacks
-export([init/1,handle_call/3,handle_cast/2,handle_info/2,terminate/2,
code_change/3]).
%%% address_db - ets() Contains mappings from a tuple {Host,Port} to a tuple
%%% {LastSID,OpenSessions,ets()} where
%%% LastSid is the last allocated session id,
%%% OpenSessions is the number of currently open sessions and
%%% ets() contains mappings from Session Id to #session{}.
%%%
%%% Note:
%%% - Only persistent connections are stored in address_db
%%% - When automatically redirecting, multiple requests are performed.
-record(state,{
address_db, % ets()
reqid % int() Next Request id to use (identifies request).
}).
%%====================================================================
%% External functions
%%====================================================================
%%--------------------------------------------------------------------
%% Function: start_link/0
%% Description: Starts the server
%%--------------------------------------------------------------------
start() ->
ensure_started().
start_link() ->
gen_server:start_link({local,?HMACALL}, ?HMANAME, [], []).
%% Find available session process and store in address_db. If no
%% available, start new handler process.
request(Req) ->
ensure_started(),
ClientClose=http_lib:connection_close(Req#request.headers),
gen_server:call(?HMACALL,{request,ClientClose,Req},infinity).
cancel_request(ReqId) ->
gen_server:call(?HMACALL,{cancel_request,ReqId},infinity).
%%% Close Session
close_session(Addr,Sid) ->
gen_server:call(?HMACALL,{close_session,Addr,Sid},infinity).
close_session(Req,Addr,Sid) ->
gen_server:call(?HMACALL,{close_session,Req,Addr,Sid},infinity).
abort_session(Addr,Sid,Msg) ->
gen_server:call(?HMACALL,{abort_session,Addr,Sid,Msg},infinity).
%%% Pick next in request que
next_request(Addr,Sid) ->
gen_server:call(?HMACALL,{next_request,Addr,Sid},infinity).
%%% Session handler has succeed to set up a new session, now register
%%% the socket
register_socket(Addr,Sid,Socket) ->
gen_server:cast(?HMACALL,{register_socket,Addr,Sid,Socket}).
%%% Debugging
status() ->
gen_server:cast(?HMACALL,status).
%%--------------------------------------------------------------------
%% Function: init/1
%% Description: Initiates the server
%% Returns: {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%%--------------------------------------------------------------------
init([]) ->
process_flag(trap_exit, true),
{ok,#state{address_db=ets:new(address_db,[private]),
reqid=0}}.
%%--------------------------------------------------------------------
%% Function: handle_call/3
%% Description: Handling call messages
%% Returns: {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} | (terminate/2 is called)
%% {stop, Reason, State} (terminate/2 is called)
%%--------------------------------------------------------------------
%%% Note:
%%% - We may have multiple non-persistent connections, each will be handled in
%%% separate processes, thus don't add such connections to address_db
handle_call({request,false,Req},_From,State) ->
case ets:lookup(State#state.address_db,Req#request.address) of
[] ->
STab=ets:new(session_db,[private,{keypos,2},set]),
case persistent_new_session_request(0,Req,STab,State) of
{Reply,LastSid,State2} ->
ets:insert(State2#state.address_db,
{Req#request.address,{LastSid,1,STab}}),
{reply,Reply,State2};
{ErrorReply,State2} ->
{reply,ErrorReply,State2}
end;
[{_,{LastSid,OpenS,STab}}] ->
case lookup_session_entry(STab) of
{ok,Session} ->
old_session_request(Session,Req,STab,State);
need_new_session when OpenS<(Req#request.settings)#client_settings.max_sessions ->
case persistent_new_session_request(LastSid,Req,
STab,State) of
{Reply,LastSid2,State2} ->
ets:insert(State2#state.address_db,
{Req#request.address,
{LastSid2,OpenS+1,STab}}),
{reply,Reply,State2};
{ErrorReply,State2} ->
{reply,ErrorReply,State2}
end;
need_new_session ->
{reply,{error,too_many_sessions},State}
end
end;
handle_call({request,true,Req},_From,State) ->
{Reply,State2}=not_persistent_new_session_request(Req,State),
{reply,Reply,State2};
handle_call({cancel_request,true,_ReqId},_From,State) ->
%% FIXME Should be possible to scan through all requests made, but perhaps
%% better to give some more hints (such as Addr etc)
Reply=ok,
{reply,Reply,State};
handle_call({next_request,Addr,Sid},_From,State) ->
case ets:lookup(State#state.address_db,Addr) of
[] ->
{reply,{error,no_connection},State};
[{_,{_,_,STab}}] ->
case ets:lookup(STab,Sid) of
[] ->
{reply,{error,session_not_registered},State};
[S=#session{pipeline=[],quelength=QueLen}] ->
if
QueLen==1 ->
ets:insert(STab,S#session{quelength=0});
true ->
ok
end,
{reply,no_more_requests,State};
[S=#session{pipeline=Que}] ->
[Req|RevQue]=lists:reverse(Que),
ets:insert(STab,S#session{pipeline=lists:reverse(RevQue),
quelength=S#session.quelength-1}),
{reply,Req,State}
end
end;
handle_call({close_session,Addr,Sid},_From,State) ->
case ets:lookup(State#state.address_db,Addr) of
[] ->
{reply,{error,no_connection},State};
[{_,{LastSid,OpenS,STab}}] ->
case ets:lookup(STab,Sid) of
[#session{pipeline=Que}] ->
R=handle_close_session(lists:reverse(Que),STab,Sid,State),
ets:insert(State#state.address_db,
{Addr,{LastSid,OpenS-1,STab}}),
{reply,R,State};
[] ->
{reply,{error,session_not_registered},State}
end
end;
handle_call({close_session,Req,Addr,Sid},_From,State) ->
case ets:lookup(State#state.address_db,Addr) of
[] ->
{reply,{error,no_connection},State};
[{_,{LastSid,OpenS,STab}}] ->
case ets:lookup(STab,Sid) of
[#session{pipeline=Que}] ->
R=handle_close_session([Req|lists:reverse(Que)],
STab,Sid,State),
ets:insert(State#state.address_db,
{Addr,{LastSid,OpenS-1,STab}}),
{reply,R,State};
[] ->
{reply,{error,session_not_registered},State}
end
end;
handle_call({abort_session,Addr,Sid,Msg},_From,State) ->
case ets:lookup(State#state.address_db,Addr) of
[] ->
{reply,{error,no_connection},State};
[{_,{LastSid,OpenS,STab}}] ->
case ets:lookup(STab,Sid) of
[#session{pipeline=Que}] ->
R=abort_request_que(Que,{error,Msg}),
ets:delete(STab,Sid),
ets:insert(State#state.address_db,
{Addr,{LastSid,OpenS-1,STab}}),
{reply,R,State};
[] ->
{reply,{error,session_not_registered},State}
end
end.
%%--------------------------------------------------------------------
%% Function: handle_cast/2
%% Description: Handling cast messages
%% Returns: {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State} (terminate/2 is called)
%%--------------------------------------------------------------------
handle_cast(status, State) ->
io:format("Status:~n"),
print_all(lists:sort(ets:tab2list(State#state.address_db))),
{noreply, State};
handle_cast({register_socket,Addr,Sid,Socket},State) ->
case ets:lookup(State#state.address_db,Addr) of
[] ->
{noreply,State};
[{_,{_,_,STab}}] ->
case ets:lookup(STab,Sid) of
[Session] ->
ets:insert(STab,Session#session{socket=Socket}),
{noreply,State};
[] ->
{noreply,State}
end
end.
print_all([]) ->
ok;
print_all([{Addr,{LastSid,OpenSessions,STab}}|Rest]) ->
io:format(" Address:~p LastSid=~p OpenSessions=~p~n",[Addr,LastSid,OpenSessions]),
SortedList=lists:sort(fun(A,B) ->
if
A#session.id<B#session.id ->
true;
true ->
false
end
end,ets:tab2list(STab)),
print_all2(SortedList),
print_all(Rest).
print_all2([]) ->
ok;
print_all2([Session|Rest]) ->
io:format(" Session:~p~n",[Session#session.id]),
io:format(" Client close:~p~n",[Session#session.clientclose]),
io:format(" Socket:~p~n",[Session#session.socket]),
io:format(" Pipe: length=~p Que=~p~n",[Session#session.quelength,Session#session.pipeline]),
print_all2(Rest).
%%--------------------------------------------------------------------
%% Function: handle_info/2
%% Description: Handling all non call/cast messages
%% Returns: {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State} (terminate/2 is called)
%%--------------------------------------------------------------------
handle_info({'EXIT',_Pid,normal}, State) ->
{noreply, State};
handle_info(Info, State) ->
io:format("ERROR httpc_manager:handle_info ~p~n",[Info]),
{noreply, State}.
%%--------------------------------------------------------------------
%% Function: terminate/2
%% Description: Shutdown the server
%% Returns: any (ignored by gen_server)
%%--------------------------------------------------------------------
terminate(_Reason, State) ->
ets:delete(State#state.address_db).
%%--------------------------------------------------------------------
%% Func: code_change/3
%% Purpose: Convert process state when code is changed
%% Returns: {ok, NewState}
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
%%% From RFC 2616, Section 8.1.4
%%% A client, server, or proxy MAY close the transport connection at any
%%% time. For example, a client might have started to send a new request
%%% at the same time that the server has decided to close the "idle"
%%% connection. From the server's point of view, the connection is being
%%% closed while it was idle, but from the client's point of view, a
%%% request is in progress.
%%%
%%% This means that clients, servers, and proxies MUST be able to recover
%%% from asynchronous close events. Client software SHOULD reopen the
%%% transport connection and retransmit the aborted sequence of requests
%%% without user interaction so long as the request sequence is
%%% idempotent (see section 9.1.2). Non-idempotent methods or sequences
%%%
%%% FIXME
%%% Note:
%%% - If this happen (server close because of idle) there can't be any requests
%%% in the que.
%%% - This is the main function for closing of sessions
handle_close_session([],STab,Sid,_State) ->
ets:delete(STab,Sid);
handle_close_session(Que,STab,Sid,_State) ->
ets:delete(STab,Sid),
abort_request_que(Que,{error,aborted_request}).
%%% From RFC 2616, Section 8.1.2.2
%%% Clients which assume persistent connections and pipeline immediately
%%% after connection establishment SHOULD be prepared to retry their
%%% connection if the first pipelined attempt fails. If a client does
%%% such a retry, it MUST NOT pipeline before it knows the connection is
%%% persistent. Clients MUST also be prepared to resend their requests if
%%% the server closes the connection before sending all of the
%%% corresponding responses.
%%% FIXME! I'm currently not checking if tis is the first attempt on the session
%%% FIXME! Pipeline size must be dynamically variable (e.g. 0 if resend, 2 else)
%%% The que contains requests that have been sent ok previously, but the session
%%% was closed prematurely when reading the response.
%%% Try setup a new session and resend these requests.
%%% Note:
%%% - This MUST be a persistent session
% handle_closed_pipelined_session_que([],_State) ->
% ok;
% handle_closed_pipelined_session_que(_Que,_State) ->
% ok.
%%% From RFC 2616, Section 8.2.4
%%% If an HTTP/1.1 client sends a request which includes a request body,
%%% but which does not include an Expect request-header field with the
%%% "100-continue" expectation, and if the client is not directly
%%% connected to an HTTP/1.1 origin server, and if the client sees the
%%% connection close before receiving any status from the server, the
%%% client SHOULD retry the request. If the client does retry this
%%% request, it MAY use the following "binary exponential backoff"
%%% algorithm to be assured of obtaining a reliable response:
%%% ...
%%% FIXME! I'm currently not checking if a "Expect: 100-continue" has been sent.
% handle_remotely_closed_session_que([],_State) ->
% ok;
% handle_remotely_closed_session_que(_Que,_State) ->
% % resend_que(Que,Socket),
% ok.
%%% Resend all requests in the request que
% resend_que([],_) ->
% ok;
% resend_que([Req|Que],Socket) ->
% case catch httpc_handler:http_request(Req,Socket) of
% ok ->
% resend_que(Que,Socket);
% {error,Reason} ->
% {error,Reason}
% end.
%%% From RFC 2616,
%%% Section 8.1.2.2:
%%% Clients SHOULD NOT pipeline requests using non-idempotent methods or
%%% non-idempotent sequences of methods (see section 9.1.2). Otherwise, a
%%% premature termination of the transport connection could lead to
%%% indeterminate results. A client wishing to send a non-idempotent
%%% request SHOULD wait to send that request until it has received the
%%% response status for the previous request.
%%% Section 9.1.2:
%%% Methods can also have the property of "idempotence" in that (aside
%%% from error or expiration issues) the side-effects of N > 0 identical
%%% requests is the same as for a single request. The methods GET, HEAD,
%%% PUT and DELETE share this property. Also, the methods OPTIONS and
%%% TRACE SHOULD NOT have side effects, and so are inherently idempotent.
%%%
%%% Note that POST and CONNECT are idempotent methods.
%%%
%%% Tries to find an open, free session i STab. Such a session has quelength
%%% less than ?MAX_PIPELINE_LENGTH
%%% Don't care about non-standard, user defined methods.
%%%
%%% Returns {ok,Session} or need_new_session where
%%% Session is the session that may be used
lookup_session_entry(STab) ->
MS=[{#session{quelength='$1',max_quelength='$2',
id='_',clientclose='_',socket='$3',scheme='_',pipeline='_'},
[{'<','$1','$2'},{is_port,'$3'}],
['$_']}],
case ets:select(STab,MS) of
[] ->
need_new_session;
SessionList -> % Now check if any of these has an empty pipeline.
case lists:keysearch(0,2,SessionList) of
{value,Session} ->
{ok,Session};
false ->
{ok,hd(SessionList)}
end
end.
%%% Returns a tuple {Reply,State} where
%%% Reply is the response sent back to the application
%%%
%%% Note:
%%% - An {error,einval} from a send should sometimes rather be {error,closed}
%%% - Don't close the session from here, let httpc_handler take care of that.
%old_session_request(Session,Req,STab,State)
% when (Req#request.settings)#client_settings.max_quelength==0 ->
% Session1=Session#session{pipeline=[Req]},
% ets:insert(STab,Session1),
% {reply,{ok,ReqId},State#state{reqid=ReqId+1}};
old_session_request(Session,Req,STab,State) ->
ReqId=State#state.reqid,
Req1=Req#request{id=ReqId},
case catch httpc_handler:http_request(Req1,Session#session.socket) of
ok ->
Session1=Session#session{pipeline=[Req1|Session#session.pipeline],
quelength=Session#session.quelength+1},
ets:insert(STab,Session1),
{reply,{ok,ReqId},State#state{reqid=ReqId+1}};
{error,Reason} ->
ets:insert(STab,Session#session{socket=undefined}),
% http_lib:close(Session#session.sockettype,Session#session.socket),
{reply,{error,Reason},State#state{reqid=ReqId+1}}
end.
%%% Returns atuple {Reply,Sid,State} where
%%% Reply is the response sent back to the application, and
%%% Sid is the last used Session Id
persistent_new_session_request(Sid,Req,STab,State) ->
ReqId=State#state.reqid,
case setup_new_session(Req#request{id=ReqId},false,Sid) of
{error,Reason} ->
{{error,Reason},State#state{reqid=ReqId+1}};
{NewSid,Session} ->
ets:insert(STab,Session),
{{ok,ReqId},NewSid,State#state{reqid=ReqId+1}}
end.
%%% Returns a tuple {Reply,State} where
%%% Reply is the response sent back to the application
not_persistent_new_session_request(Req,State) ->
ReqId=State#state.reqid,
case setup_new_session(Req#request{id=ReqId},true,undefined) of
{error,Reason} ->
{{error,Reason},State#state{reqid=ReqId+1}};
ok ->
{{ok,ReqId},State#state{reqid=ReqId+1}}
end.
%%% As there are no sessions available, setup a new session and send the request
%%% on it.
setup_new_session(Req,ClientClose,Sid) ->
S=#session{id=Sid,clientclose=ClientClose,
scheme=Req#request.scheme,
max_quelength=(Req#request.settings)#client_settings.max_quelength},
spawn_link(httpc_handler,init_connection,[Req,S]),
case ClientClose of
false ->
{Sid+1,S};
true ->
ok
end.
%%% ----------------------------------------------------------------------------
%%% Abort all requests in the request que.
abort_request_que([],_Msg) ->
ok;
abort_request_que([#request{from=From,ref=Ref,id=Id}|Que],Msg) ->
gen_server:cast(From,{Ref,Id,Msg}),
abort_request_que(Que,Msg);
abort_request_que(#request{from=From,ref=Ref,id=Id},Msg) ->
gen_server:cast(From,{Ref,Id,Msg}).
%%% --------------------------------
% C={httpc_manager,{?MODULE,start_link,[]},permanent,1000,
% worker,[?MODULE]},
% supervisor:start_child(inets_sup, C),
ensure_started() ->
case whereis(?HMANAME) of
undefined ->
start_link();
_ ->
ok
end.
%%% ============================================================================
%%% This is deprecated code, to be removed
% format_time() ->
% {_,_,MicroSecs}=TS=now(),
% {{Y,Mon,D},{H,M,S}}=calendar:now_to_universal_time(TS),
% lists:flatten(io_lib:format("~4.4.0w-~2.2.0w-~2.2.0w,~2.2.0w:~2.2.0w:~6.3.0f",
% [Y,Mon,D,H,M,S+(MicroSecs/1000000)])).
|