aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dialyzer/test/r9c_SUITE_data/src/inets/httpd_request_handler.erl
blob: ebda5c6562508854c7c5dbe86a04fd9f06199e9e (plain) (blame)
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
%% ``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 Ericsson Utvecklings AB.
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
%% AB. All Rights Reserved.''
%%
%%     $Id: httpd_request_handler.erl,v 1.1 2008/12/17 09:53:34 mikpe Exp $
-module(httpd_request_handler).

%% app internal api
-export([start_link/2, synchronize/3]).

%% module internal api
-export([connection/2, do_next_connection/6, read_header/7]).
-export([parse_trailers/1, newline/1]).

-include("httpd.hrl").
-include("httpd_verbosity.hrl").


%% start_link

start_link(Manager, ConfigDB) ->
    Pid = proc_lib:spawn(?MODULE, connection, [Manager, ConfigDB]),
    {ok, Pid}.


%% synchronize

synchronize(Pid, SocketType, Socket) ->
    Pid ! {synchronize, SocketType, Socket}.

% connection

connection(Manager, ConfigDB) ->
    {SocketType, Socket, {Status, Verbosity}} = await_synchronize(Manager),
    put(sname,self()),
    put(verbosity,?vvalidate(Verbosity)),
    connection1(Status, Manager, ConfigDB, SocketType, Socket).


connection1({reject, busy}, Manager, ConfigDB, SocketType, Socket) ->
    handle_busy(Manager, ConfigDB, SocketType, Socket);

connection1({reject, blocked}, Manager, ConfigDB, SocketType, Socket) ->
    handle_blocked(Manager, ConfigDB, SocketType, Socket);

connection1(accept, Manager, ConfigDB, SocketType, Socket) ->
    handle_connection(Manager, ConfigDB, SocketType, Socket).


%% await_synchronize

await_synchronize(Manager) ->
    receive
	{synchronize, SocketType, Socket} ->
	    ?vlog("received syncronize: "
		  "~n   SocketType: ~p"
		  "~n   Socket:     ~p", [SocketType, Socket]),
	    {SocketType, Socket, httpd_manager:new_connection(Manager)}
    after 5000 ->
	    exit(synchronize_timeout)
    end.


% handle_busy

handle_busy(Manager, ConfigDB, SocketType, Socket) ->
    ?vlog("handle busy: ~p", [Socket]),
    MaxClients = httpd_util:lookup(ConfigDB, max_clients, 150),
    String = io_lib:format("heavy load (>~w processes)", [MaxClients]),
    reject_connection(Manager, ConfigDB, SocketType, Socket, String).


% handle_blocked

handle_blocked(Manager, ConfigDB, SocketType, Socket) ->
    ?vlog("handle blocked: ~p", [Socket]),
    String = "Server maintenance performed, try again later",
    reject_connection(Manager, ConfigDB, SocketType, Socket, String).


% reject_connection

reject_connection(Manager, ConfigDB, SocketType, Socket, Info) ->
    String = lists:flatten(Info),
    ?vtrace("send status (503) message", []),
    httpd_response:send_status(SocketType, Socket, 503, String, ConfigDB),
    %% This ugly thing is to make ssl deliver the message, before the close...
    close_sleep(SocketType, 1000),
    ?vtrace("close the socket", []),
    close(SocketType, Socket, ConfigDB).


% handle_connection

handle_connection(Manager, ConfigDB, SocketType, Socket) ->
    ?vlog("handle connection: ~p", [Socket]),
    Resolve     = httpd_socket:resolve(SocketType),
    Peername    = httpd_socket:peername(SocketType, Socket),
    InitData    = #init_data{peername=Peername, resolve=Resolve},
    TimeOut     = httpd_util:lookup(ConfigDB, keep_alive_timeout, 150000),
    NrOfRequest = httpd_util:lookup(ConfigDB, max_keep_alive_request, forever),
    ?MODULE:do_next_connection(ConfigDB, InitData,
			       SocketType, Socket,NrOfRequest,TimeOut),
    ?vlog("handle connection: done", []),
    httpd_manager:done_connection(Manager),
    ?vlog("handle connection: close socket", []),
    close(SocketType, Socket, ConfigDB).


% do_next_connection
do_next_connection(_ConfigDB, _InitData, _SocketType, _Socket, NrOfRequests,
		   _Timeout) when NrOfRequests < 1 ->
    ?vtrace("do_next_connection: done", []),
    ok;
do_next_connection(ConfigDB, InitData, SocketType, Socket, NrOfRequests,
		   Timeout) ->
    Peername = InitData#init_data.peername,
    case (catch read(ConfigDB, SocketType, Socket, InitData, Timeout)) of
        {'EXIT', Reason} ->
            ?vlog("exit reading from socket: ~p",[Reason]),
            error_logger:error_report({'EXIT',Reason}),
	    String =
		lists:flatten(
		  io_lib:format("exit reading from socket: ~p => ~n~p~n",
				[Socket, Reason])),
	    error_log(mod_log,
		      SocketType, Socket, ConfigDB, Peername, String),
	    error_log(mod_disk_log,
		      SocketType, Socket, ConfigDB, Peername, String);
        {error, Reason} ->
            handle_read_error(Reason,SocketType,Socket,ConfigDB,Peername);
        Info when record(Info, mod) ->
            case Info#mod.connection of
                true ->
                    ReqTimeout = httpd_util:lookup(ConfigDB,
						   keep_alive_timeout, 150000),
		    ?MODULE:do_next_connection(ConfigDB,          InitData,
					       SocketType,        Socket,
					       dec(NrOfRequests), ReqTimeout);
		_ ->
                    ok
            end;
        _ ->
            ok
    end.



%% read
read(ConfigDB, SocketType, Socket, InitData, Timeout) ->
    ?vdebug("read from socket ~p with Timeout ~p",[Socket, Timeout]),
    MaxHdrSz = httpd_util:lookup(ConfigDB, max_header_size, 10240),
    case ?MODULE:read_header(SocketType, Socket, Timeout, MaxHdrSz,
			     ConfigDB, InitData, []) of
	{socket_closed, Reason} ->
	    ?vlog("Socket closed while reading request header: "
		  "~n   ~p", [Reason]),
	    socket_close;
	{error, Error} ->
	    {error, Error};
	{ok, Info, EntityBodyPart} ->
	    read1(SocketType, Socket, ConfigDB, InitData, Timeout, Info,
		  EntityBodyPart)
    end.

%% Got the head and maybe a part of the body: read in the rest
read1(SocketType, Socket, ConfigDB, InitData, Timeout, Info, BodyPart)->
    MaxBodySz     = httpd_util:lookup(ConfigDB, max_body_size, nolimit),
    ContentLength = content_length(Info),
    ?vtrace("ContentLength: ~p", [ContentLength]),
    case read_entity_body(SocketType, Socket, Timeout, MaxBodySz,
			  ContentLength, BodyPart, Info, ConfigDB) of
	{socket_closed, Reason} ->
	    ?vlog("Socket closed while reading request body: "
		  "~n   ~p", [Reason]),
	    socket_close;
	{ok, EntityBody} ->
	    finish_request(EntityBody, [], Info);
	{ok, ExtraHeader, EntityBody} ->
	    finish_request(EntityBody, ExtraHeader, Info);
	Response ->
	    httpd_socket:close(SocketType, Socket),
	    socket_closed
	    %% Catch up all bad return values
    end.


%% The request is read in send it forward to the module that
%% generates the response

finish_request(EntityBody, ExtraHeader,
	       #mod{parsed_header = ParsedHeader} = Info)->
    ?DEBUG("finish_request -> ~n"
	"    EntityBody:   ~p~n"
	"    ExtraHeader:  ~p~n"
	"    ParsedHeader: ~p~n",
	[EntityBody, ExtraHeader, ParsedHeader]),
    httpd_response:send(Info#mod{parsed_header = ParsedHeader ++ ExtraHeader,
				 entity_body   = EntityBody}).


%% read_header

%% This algorithm rely on the buffer size of the inet driver together
%% with the {active, once} socket option. Atmost one message of this
%% size will be received at a given time. When a full header has been
%% read, the body is read with the recv function (the body size is known).
%%
read_header(SocketType, Socket, Timeout, MaxHdrSz, ConfigDB,
	    InitData, SoFar0) ->
    T = t(),
    %% remove any newlines at the begining, they might be crap from ?
    SoFar = remove_newline(SoFar0),

    case terminated_header(MaxHdrSz, SoFar) of
	{true, Header, EntityBodyPart} ->
	    ?vdebug("read_header -> done reading header: "
		    "~n   length(Header):         ~p"
		    "~n   length(EntityBodyPart): ~p",
		    [length(Header), length(EntityBodyPart)]),
	    transform_header(SocketType, Socket, Header, ConfigDB, InitData,
			     EntityBodyPart);
	false ->
	    ?vtrace("read_header -> "
		    "~n   set active = 'once' and "
		    "await a chunk of the header", []),

	    case httpd_socket:active_once(SocketType, Socket) of
		ok ->
		    receive
			%%
			%% TCP
			%%
			{tcp, Socket, Data} ->
			    ?vtrace("read_header(ip) -> got some data: ~p",
				[sz(Data)]),
			    ?MODULE:read_header(SocketType, Socket,
						Timeout - (t()-T),
						MaxHdrSz, ConfigDB,
						InitData, SoFar ++ Data);
			{tcp_closed, Socket} ->
			    ?vtrace("read_header(ip) -> socket closed",[]),
			    {socket_closed,normal};
			{tcp_error, Socket, Reason} ->
			    ?vtrace("read_header(ip) -> socket error: ~p",
				[Reason]),
			    {socket_closed, Reason};

			%%
			%% SSL
			%%
			{ssl, Socket, Data} ->
			    ?vtrace("read_header(ssl) -> got some data: ~p",
				[sz(Data)]),
			    ?MODULE:read_header(SocketType, Socket,
						Timeout - (t()-T),
						MaxHdrSz, ConfigDB,
						InitData, SoFar ++ Data);
			{ssl_closed, Socket} ->
			    ?vtrace("read_header(ssl) -> socket closed", []),
			    {socket_closed, normal};
			{ssl_error, Socket, Reason} ->
			    ?vtrace("read_header(ssl) -> socket error: ~p",
				[Reason]),
			    {socket_closed, Reason}

		    after Timeout ->
			    ?vlog("read_header -> timeout", []),
			    {socket_closed, timeout}
		    end;

		Error ->
		    httpd_response:send_status(SocketType, Socket,
					       500, none, ConfigDB),
		    Error
	    end
    end.


terminated_header(MaxHdrSz, Data) ->
    D1 = lists:flatten(Data),
    ?vtrace("terminated_header -> Data size: ~p",[sz(D1)]),
    case hsplit(MaxHdrSz,[],D1) of
	not_terminated ->
	    false;
	[Header, EntityBodyPart] ->
	    {true, Header++"\r\n\r\n",EntityBodyPart}
    end.


transform_header(SocketType, Socket, Request, ConfigDB, InitData, BodyPart) ->
    case httpd_parse:request_header(Request) of
	{not_implemented, RequestLine, Method, RequestURI, ParsedHeader,
	 HTTPVersion} ->
	    httpd_response:send_status(SocketType, Socket, 501,
				       {Method, RequestURI, HTTPVersion},
				       ConfigDB),
	    {error,"Not Implemented"};
	{bad_request, {forbidden, URI}} ->
	    httpd_response:send_status(SocketType, Socket, 403, URI, ConfigDB),
	    {error,"Forbidden Request"};
	{bad_request, Reason} ->
	    httpd_response:send_status(SocketType, Socket, 400, none,
				       ConfigDB),
	    {error,"Malformed request"};
	{ok,[Method, RequestURI, HTTPVersion, RequestLine, ParsedHeader]} ->
	    ?DEBUG("send -> ~n"
		   "    Method:      ~p~n"
		   "    RequestURI:  ~p~n"
		   "    HTTPVersion: ~p~n"
		   "    RequestLine: ~p~n",
		   [Method, RequestURI, HTTPVersion, RequestLine]),
	    {ok, Info} =
		httpd_parse:get_request_record(Socket, SocketType, ConfigDB,
					       Method, RequestURI, HTTPVersion,
					       RequestLine, ParsedHeader,
					       [], InitData),
	    %% Control that the Host header field is provided
	    case Info#mod.absolute_uri of
		nohost ->
		    case Info#mod.http_version of
			"HTTP/1.1" ->
			    httpd_response:send_status(Info, 400, none),
			    {error,"No host specified"};
			_ ->
			    {ok, Info, BodyPart}
		    end;
		_ ->
		    {ok, Info, BodyPart}
	    end
    end.


hsplit(_MaxHdrSz, Accu,[]) ->
    not_terminated;
hsplit(_MaxHdrSz, Accu, [ $\r, $\n, $\r, $\n | Tail]) ->
    [lists:reverse(Accu), Tail];
hsplit(nolimit, Accu, [H|T]) ->
    hsplit(nolimit,[H|Accu],T);
hsplit(MaxHdrSz, Accu, [H|T]) when length(Accu) < MaxHdrSz ->
    hsplit(MaxHdrSz,[H|Accu],T);
hsplit(MaxHdrSz, Accu, D) ->
    throw({error,{header_too_long,length(Accu),length(D)}}).



%%----------------------------------------------------------------------
%% The http/1.1 standard chapter 8.2.3 says that a request containing
%% An Except header-field must be responded to by 100 (Continue) by
%% the server before the client sends the body.
%%----------------------------------------------------------------------

read_entity_body(SocketType, Socket, Timeout, Max, Length, BodyPart, Info,
		 ConfigDB) when integer(Max) ->
    case expect(Info#mod.http_version, Info#mod.parsed_header, ConfigDB) of
	continue when Max > Length ->
	    ?DEBUG("read_entity_body()->100 Continue  ~n", []),
	    httpd_response:send_status(Info, 100, ""),
	    read_entity_body2(SocketType, Socket, Timeout, Max, Length,
			      BodyPart, Info, ConfigDB);
	continue when Max < Length ->
	    httpd_response:send_status(Info, 417, "Body to big"),
	    httpd_socket:close(SocketType, Socket),
	    {socket_closed,"Expect denied according to size"};
	break ->
	    httpd_response:send_status(Info, 417, "Method not allowed"),
	    httpd_socket:close(SocketType, Socket),
	    {socket_closed,"Expect conditions was not fullfilled"};
	no_expect_header ->
	    read_entity_body2(SocketType, Socket, Timeout, Max, Length,
			      BodyPart, Info, ConfigDB);
	http_1_0_expect_header ->
	    httpd_response:send_status(Info, 400,
				       "Only HTTP/1.1 Clients "
				       "may use the Expect Header"),
	    httpd_socket:close(SocketType, Socket),
	    {socket_closed,"Due to a HTTP/1.0 expect header"}
    end;

read_entity_body(SocketType, Socket, Timeout, Max, Length, BodyPart,
		 Info, ConfigDB) ->
    case expect(Info#mod.http_version, Info#mod.parsed_header, ConfigDB) of
	continue ->
	    ?DEBUG("read_entity_body() -> 100 Continue  ~n", []),
	    httpd_response:send_status(Info, 100, ""),
	    read_entity_body2(SocketType, Socket, Timeout, Max, Length,
			      BodyPart, Info, ConfigDB);
	break->
	    httpd_response:send_status(Info, 417, "Method not allowed"),
	    httpd_socket:close(SocketType, Socket),
	    {socket_closed,"Expect conditions was not fullfilled"};
	no_expect_header ->
	    read_entity_body2(SocketType, Socket, Timeout, Max, Length,
			      BodyPart, Info, ConfigDB);
	http_1_0_expect_header ->
	    httpd_response:send_status(Info, 400,
				       "HTTP/1.0 Clients are not allowed "
				       "to use the Expect Header"),
	    httpd_socket:close(SocketType, Socket),
	    {socket_closed,"Expect header field in an HTTP/1.0 request"}
	end.

%%----------------------------------------------------------------------
%% control if the body is transfer encoded
%%----------------------------------------------------------------------
read_entity_body2(SocketType, Socket, Timeout, Max, Length, BodyPart,
		  Info, ConfigDB) ->
    ?DEBUG("read_entity_body2() -> "
	"~n   Max:    ~p"
	"~n   Length: ~p"
	"~n   Socket: ~p", [Max, Length, Socket]),

    case transfer_coding(Info) of
	{chunked, ChunkedData} ->
	    ?DEBUG("read_entity_body2() -> "
		"Transfer-encoding: Chunked Data: BodyPart ~s", [BodyPart]),
	    read_chunked_entity(Info, Timeout, Max, Length, ChunkedData, [],
				BodyPart);
	unknown_coding ->
	    ?DEBUG("read_entity_body2() -> Transfer-encoding: Unknown",[]),
	    httpd_response:send_status(Info, 501, "Unknown Transfer-Encoding"),
	    httpd_socket:close(SocketType, Socket),
	    {socket_closed,"Expect conditions was not fullfilled"};
	none ->
	      ?DEBUG("read_entity_body2() -> Transfer-encoding: none",[]),
	    read_entity_body(SocketType, Socket, Timeout, Max, Length,
			     BodyPart)
    end.


%%----------------------------------------------------------------------
%% The body was plain read it from the socket
%% ----------------------------------------------------------------------
read_entity_body(_SocketType, _Socket, _Timeout, _Max, 0, _BodyPart) ->
    {ok, []};

read_entity_body(_SocketType, _Socket, _Timeout, Max, Len, _BodyPart)
  when Max < Len ->
    ?vlog("body to long: "
	  "~n   Max: ~p"
	  "~n   Len: ~p", [Max,Len]),
    throw({error,{body_too_long,Max,Len}});

%% OTP-4409: Fixing POST problem
read_entity_body(_,_,_,_, Len, BodyPart) when Len == length(BodyPart) ->
    ?vtrace("read_entity_body -> done when"
	"~n   Len = length(BodyPart): ~p", [Len]),
    {ok, BodyPart};

%% OTP-4550: Fix problem with trailing garbage produced by some clients.
read_entity_body(_, _, _, _, Len, BodyPart) when Len < length(BodyPart) ->
    ?vtrace("read_entity_body -> done when"
	"~n   Len:              ~p"
	"~n   length(BodyPart): ~p", [Len, length(BodyPart)]),
    {ok, lists:sublist(BodyPart,Len)};

read_entity_body(SocketType, Socket, Timeout, Max, Len, BodyPart) ->
    ?vtrace("read_entity_body -> entry when"
	"~n   Len:              ~p"
	"~n   length(BodyPart): ~p", [Len, length(BodyPart)]),
    %% OTP-4548:
    %% The length calculation was previously (inets-2.*) done in the
    %% read function. As of 3.0 it was removed from read but not
    %% included here.
    L = Len - length(BodyPart),
    case httpd_socket:recv(SocketType, Socket, L, Timeout) of
	{ok, Body} ->
	    ?vtrace("read_entity_body -> received some data:"
		"~n   length(Body): ~p", [length(Body)]),
	    {ok, BodyPart ++ Body};
	{error,closed} ->
	    {socket_closed,normal};
	{error,etimedout} ->
	    {socket_closed, timeout};
	{error,Reason} ->
	    {socket_closed, Reason};
	Other ->
	    {socket_closed, Other}
    end.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% If the body of the message is encoded used the chunked transfer encoding
%% it looks somethin like this:
%% METHOD URI HTTP/VSN
%% Transfer-Encoding: chunked
%% CRLF
%% ChunkSize
%% Chunk
%% ChunkSize
%% Chunk
%% 0
%% Trailer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

read_chunked_entity(Info, Timeout, Max, Length, ChunkedData, Body, []) ->
    ?DEBUG("read_chunked_entity()->:no_chunks ~n", []),
    read_chunked_entity(Info#mod.socket_type, Info#mod.socket,
			Timeout, Max, Length, ChunkedData, Body,
			Info#mod.config_db, Info);

read_chunked_entity(Info, Timeout, Max, Length, ChunkedData, Body, BodyPart) ->
    %% Get the size
    ?DEBUG("read_chunked_entity() -> PrefetchedBodyPart: ~p ~n",[BodyPart]),
    case parse_chunk_size(Info, Timeout, BodyPart) of
	{ok, Size, NewBodyPart} when Size > 0 ->
	    ?DEBUG("read_chunked_entity() -> Size: ~p ~n", [Size]),
	    case parse_chunked_entity_body(Info, Timeout, Max, length(Body),
					   Size, NewBodyPart) of
		{ok, Chunk, NewBodyPart1} ->
		    ?DEBUG("read_chunked_entity()->Size: ~p ~n", [Size]),
		    read_chunked_entity(Info, Timeout, Max, Length,
					ChunkedData, Body ++ Chunk,
					NewBodyPart1);
		OK ->
		    httpd_socket:close(Info#mod.socket_type, Info#mod.socket),
		    {socket_closed, error}
	    end;
	{ok, 0, Trailers} ->
	   ?DEBUG("read_chunked_entity()->Size: 0, Trailers: ~s Body: ~s ~n",
		[Trailers, Body]),
	    case parse_chunk_trailer(Info, Timeout, Info#mod.config_db,
				     Trailers) of
		{ok, TrailerFields} ->
		    {ok, TrailerFields, Body};
		_->
		    {ok, []}
	    end;
	Error ->
	    Error
    end.


parse_chunk_size(Info, Timeout, BodyPart) ->
    case httpd_util:split(remove_newline(BodyPart), "\r\n", 2) of
	{ok, [Size, Body]} ->
	    ?DEBUG("parse_chunk_size()->Size: ~p ~n", [Size]),
	    {ok, httpd_util:hexlist_to_integer(Size), Body};
	{ok, [Size]} ->
	    ?DEBUG("parse_chunk_size()->Size: ~p ~n", [Size]),
	    Sz = get_chunk_size(Info#mod.socket_type,
				Info#mod.socket, Timeout,
				lists:reverse(Size)),
	    {ok, Sz, []}
    end.

%%----------------------------------------------------------------------
%% We got the chunk size get the chunk
%%
%% Max:     Max numbers of bytes to read may also be undefined
%% Length:  Numbers of bytes already read
%% Size     Numbers of byte to read for the chunk
%%----------------------------------------------------------------------

%% body to big
parse_chunked_entity_body(Info, Timeout, Max, Length, Size, BodyPart)
  when Max =< (Length + Size) ->
    {error, body_to_big};

%% Prefetched body part is bigger than the current chunk
%% (i.e. BodyPart includes more than one chunk)
parse_chunked_entity_body(Info, Timeout, Max, Length, Size, BodyPart)
  when (Size+2) =< length(BodyPart) ->
    Chunk = string:substr(BodyPart, 1, Size),
    Rest  = string:substr(BodyPart, Size+3),
    ?DEBUG("parse_chunked_entity_body() -> ~nChunk: ~s ~nRest: ~s ~n",
	[Chunk, Rest]),
    {ok, Chunk, Rest};


%% We just got a part of the current chunk
parse_chunked_entity_body(Info, Timeout, Max, Length, Size, BodyPart) ->
    %% OTP-4551:
    %% Subtracting BodyPart from Size does not produce an integer
    %% when BodyPart is a list...
    Remaining = Size - length(BodyPart),
    LastPartOfChunk = read_chunked_entity_body(Info#mod.socket_type,
					       Info#mod.socket,
					       Timeout, Max,
					       Length, Remaining),
    %% Remove newline
    httpd_socket:recv(Info#mod.socket_type, Info#mod.socket, 2, Timeout),
    ?DEBUG("parse_chunked_entity_body() -> "
	"~nBodyPart: ~s"
	"~nLastPartOfChunk: ~s  ~n",
	[BodyPart, LastPartOfChunk]),
    {ok, BodyPart ++ LastPartOfChunk, []}.


%%----------------------------------------------------------------------
%% If the data we got along with the header contained the whole chunked body
%% It may aswell contain the trailer :-(
%%----------------------------------------------------------------------
%% Either trailer begins with  \r\n and then all data is there or
%% The trailer has data  then read upto \r\n\r\n
parse_chunk_trailer(Info,Timeout,ConfigDB,"\r\n")->
    {ok,[]};
parse_chunk_trailer(Info,Timeout,ConfigDB,Trailers) ->
    ?DEBUG("parse_chunk_trailer()->Trailers: ~s ~n", [Trailers]),
    case string:rstr(Trailers,"\r\n\r\n") of
	0 ->
	    MaxHdrSz=httpd_util:lookup(ConfigDB, max_header_size, 10240),
	    read_trailer_end(Info,Timeout,MaxHdrSz,Trailers);
	_->
	    %%We got the whole header parse it up
	    parse_trailers(Trailers)
    end.

parse_trailers(Trailer)->
    ?DEBUG("parse_trailer()->Trailer: ~s",[Trailer]),
    {ok,[Fields0|Crap]}=httpd_util:split(Trailer,"\r\n\r\n",2),
    Fields=string:tokens(Fields0,"\r\n"),
	[getTrailerField(X)||X<-Fields,lists:member($:,X)].


read_trailer_end(Info,Timeout,MaxHdrSz,[])->
    ?DEBUG("read_trailer_end()->[]",[]),
    case read_trailer(Info#mod.socket_type,Info#mod.socket,
		 Timeout,MaxHdrSz,[],[],
		      httpd_util:key1search(Info#mod.parsed_header,"trailer",[])) of
	{ok,Trailers}->
	    Trailers;
	_->
	    []
    end;
read_trailer_end(Info,Timeout,MaxHdrSz,Trailers)->
    ?DEBUG("read_trailer_end()->Trailers: ~s ~n ",[Trailers]),
    %% Get the last paart of the the last headerfield
    End=lists:reverse(lists:takewhile(fun(X)->case X of 10 ->false;13->false;_ ->true end end,lists:reverse(Trailers))),
    Fields0=regexp:split(Trailers,"\r\n"),
    %%Get rid of the last header field
    [_Last|Fields]=lists:reverse(Fields0),
    Headers=[getTrailerField(X)||X<-Fields,lists:member($:,X)],
    case read_trailer(Info#mod.socket_type,Info#mod.socket,
		 Timeout,MaxHdrSz,Headers,End,
		      httpd_util:key1search(Info#mod.parsed_header,"trailer",[])) of
	{ok,Trailers}->
	    Trailers;
	_->
	    []
    end.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% The code below is a a good way to read in chunked encoding but
%% that require that the encoding comes from a stream and not from a list
%%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

%%----------------------------------------------------------------------
%% The body is encoded by chubnked encoding read it in
%% ChunkedData= Chunked extensions
%% Body= the inread chunked body
%% Max:     Max numbers of bytes to read
%% Length:  Numbers of bytes already readed
%% Size     Numbers of byte to read for the chunk
%%----------------------------------------------------------------------



read_chunked_entity(SocketType, Socket, Timeout, Max, Length, ChunkedData,
		    Body, ConfigDB, Info) ->
    T = t(),
    case get_chunk_size(SocketType,Socket,Timeout,[]) of
	Size when integer(Size), Size>0 ->
	    case read_chunked_entity_body(SocketType, Socket,
					  Timeout-(t()-T),
					  Max, length(Body), Size) of
		{ok,Chunk} ->
		    ?DEBUG("read_chunked_entity/9 Got a chunk: ~p " ,[Chunk]),
		    %% Two bytes are left of the chunk, that is the CRLF
		    %% at the end that is not a part of the message
		    %% So we read it and do nothing with it.
		    httpd_socket:recv(SocketType,Socket,2,Timeout-(t()-T)),
		    read_chunked_entity(SocketType, Socket, Timeout-(t()-T),
					Max, Length, ChunkedData, Body++Chunk,
					ConfigDB, Info);
		Error ->
		    ?DEBUG("read_chunked_entity/9 Error: ~p " ,[Error]),
		    httpd_socket:close(SocketType,Socket),
		    {socket_closed,error}
	    end;
	Size when integer(Size), Size == 0 ->
	    %% Must read in any trailer fields here
	    read_chunk_trailer(SocketType, Socket, Timeout,
			       Max, Info, ChunkedData, Body, ConfigDB);
	Error ->
	    Error
    end.


%% If a user wants to send header data after the chunked data we
%% must pick it out
read_chunk_trailer(SocketType, Socket, Timeout, Max, Info, ChunkedData,
		   Body, ConfigDB) ->
    ?DEBUG("read_chunk_trailer/8: ~p " ,[Body]),
    MaxHdrSz = httpd_util:lookup(ConfigDB,max_header_size,10240),
    case httpd_util:key1search(Info#mod.parsed_header,"trailer")of
	undefined ->
	    {ok,Body};
	Fields ->
	    case read_trailer(SocketType, Socket, Timeout,
			      MaxHdrSz, [], [],
			      string:tokens(
				httpd_util:to_lower(Fields),",")) of
		{ok,[]} ->
		    {ok,Body};
		{ok,HeaderFields} ->
		    % ParsedExtraHeaders =
		    % httpd_parse:tagup_header(httpd_parse:split_lines(HeaderFields)),
		    {ok,HeaderFields,Body};
		Error ->
		    Error
	    end
    end.

read_chunked_entity_body(SocketType, Socket, Timeout, Max, Length, Size)
  when integer(Max) ->
    read_entity_body(SocketType, Socket, Timeout, Max-Length, Size, []);

read_chunked_entity_body(SocketType, Socket, Timeout, Max, _Length, Size) ->
    read_entity_body(SocketType, Socket, Timeout, Max, Size, []).

%% If we read in the \r\n the httpd_util:hexlist_to_integer
%% Will remove it and we get rid of it emmediatly :-)
get_chunk_size(SocketType, Socket, Timeout, Size) ->
    T = t(),
    ?DEBUG("get_chunk_size: ~p " ,[Size]),
    case httpd_socket:recv(SocketType,Socket,1,Timeout) of
	{ok,[Digit]} when Digit==$\n ->
	    httpd_util:hexlist_to_integer(lists:reverse(Size));
       {ok,[Digit]} ->
	    get_chunk_size(SocketType,Socket,Timeout-(t()-T),[Digit|Size]);
	{error,closed} ->
	    {socket_closed,normal};
	{error,etimedout} ->
	    {socket_closed, timeout};
	{error,Reason} ->
	    {socket_closed, Reason};
	Other ->
	    {socket_closed,Other}
    end.




%%----------------------------------------------------------------------
%% Reads the HTTP-trailer
%% Would be easy to tweak the read_head to do this but in this way
%% the chunked encoding can be updated better.
%%----------------------------------------------------------------------


%% When end is reached
%% read_trailer(SocketType,Socket,Timeout,MaxHdrSz,Headers,Last,[]) ->
%%    {ok,Headers};

%% When header to big
read_trailer(_,_,_,MaxHdrSz,Headers,Bs,_Fields)
  when MaxHdrSz < length(Headers) ->
    ?vlog("header to long: "
	  "~n   MaxHdrSz:   ~p"
	  "~n   length(Bs): ~p", [MaxHdrSz,length(Bs)]),
    throw({error,{header_too_long,MaxHdrSz,length(Bs)}});

%% The last Crlf is there
read_trailer(_, _, _, _, Headers, [$\n, $\r], _) ->
    {ok,Headers};

read_trailer(SocketType, Socket, Timeout, MaxHdrSz, Headers,
	     [$\n, $\r|Rest], Fields) ->
    case getTrailerField(lists:reverse(Rest))of
	{error,Reason}->
	    {error,"Bad trailer"};
	{HeaderField,Value}->
	    case lists:member(HeaderField,Fields) of
		true ->
		    read_trailer(SocketType,Socket,Timeout,MaxHdrSz,
				 [{HeaderField,Value} |Headers],[],
				 lists:delete(HeaderField,Fields));
		false ->
		    read_trailer(SocketType,Socket,Timeout,MaxHdrSz,
				 Headers,[],Fields)
	    end
    end;

% read_trailer(SocketType,Socket,Timeout,MaxHdrSz,Headers,[$\n, $\r|Rest],Fields) ->
%     case Rest of
% 	[] ->
% 	   read_trailer(SocketType,Socket,Timeout,MaxHdrSz,Headers,Rest,Fields);
% 	Field ->
% 	    case getTrailerField(lists:reverse(Rest))of
% 		{error,Reason}->
% 		    {error,"Bad trailer"};
% 		{HeaderField,Value}->
% 		    case lists:member(HeaderField,Fields) of
% 			true ->
% 			    read_trailer(SocketType,Socket,Timeout,MaxHdrSz,
% 					 [{HeaderField,Value} |Headers],[],
% 					 lists:delete(HeaderField,Fields));
% 			false ->
% 			    read_trailer(SocketType,Socket,Timeout,MaxHdrSz,
% 					 Headers,[],Fields)
% 		    end
% 	    end
%     end;

read_trailer(SocketType,Socket,Timeout,MaxHdrSz,Headers,Bs,Fields) ->
    %% ?vlog("read_header -> entry with Timeout: ~p",[Timeout]),
    T = t(),
    case (catch httpd_socket:recv(SocketType,Socket,1,Timeout)) of
	{ok,[B]} ->
	    read_trailer(SocketType, Socket, Timeout-(t()-T),
			 MaxHdrSz, Headers, [B|Bs], Fields);
	{error,closed} ->
	    {socket_closed,normal};
	{error,etimedout} ->
	    {socket_closed, timeout};
	{error,Reason} ->
	    {socket_closed, Reason};
	Other ->
	    {socket_closed,Other}
    end.

getTrailerField(HeaderField)->
   case string:str(HeaderField,":") of
       0->
	   {error,"badheaderfield"};
       Number ->
	   {httpd_util:to_lower(string:substr(HeaderField,1,Number-1)),
	    httpd_util:to_lower(string:substr(HeaderField,Number+1))}
   end.




%% Time in milli seconds
t() ->
    {A,B,C} = erlang:now(),
    A*1000000000+B*1000+(C div 1000).

%%----------------------------------------------------------------------
%% If the user sends an expect header-field with the value 100-continue
%% We must send a 100 status message if he is a HTTP/1.1 client.

%% If it is an HTTP/1.0 client it's little more difficult.
%% If expect is not defined it is easy but in the other case shall we
%% Break or the transmission or let it continue the standard is not clear
%% if to break connection or wait for data.
%%----------------------------------------------------------------------
expect(HTTPVersion,ParsedHeader,ConfigDB)->
    case HTTPVersion of
	[$H,$T,$T,$P,$\/,$1,$.,N|_Whatever]when N>=1->
	    case httpd_util:key1search(ParsedHeader,"expect") of
		"100-continue" ->
		    continue;
		undefined ->
		    no_expect_header;
		NewValue ->
		    break
	    end;
	_OldVersion ->
	    case httpd_util:key1search(ParsedHeader,"expect") of
		undefined ->
		    no_expect_header;
		NewValue ->
		    case httpd_util:lookup(ConfigDB,expect,continue) of
			continue->
			    no_expect_header;
			_ ->
			    http_1_0_expect_header
		    end
	    end
    end.


%%----------------------------------------------------------------------
%% According to the http/1.1 standard all applications must understand
%% Chunked encoded data. (Last line chapter 3.6.1).
transfer_coding(#mod{parsed_header = Ph}) ->
    case httpd_util:key1search(Ph, "transfer-encoding", none) of
	none ->
	    none;
	[$c,$h,$u,$n,$k,$e,$d|Data]->
	    {chunked,Data};
	_ ->
	    unknown_coding
    end.



handle_read_error({header_too_long,Max,Rem},
                  SocketType,Socket,ConfigDB,Peername) ->
    String = io_lib:format("header too long: ~p : ~p",[Max,Rem]),
    handle_read_error(ConfigDB,String,SocketType,Socket,Peername,
                      max_header_action,close);
handle_read_error({body_too_long,Max,Actual},
                  SocketType,Socket,ConfigDB,Peername) ->
    String = io_lib:format("body too long: ~p : ~p",[Max,Actual]),
    handle_read_error(ConfigDB,String,SocketType,Socket,Peername,
                      max_body_action,close);
handle_read_error(Error,SocketType,Socket,ConfigDB,Peername) ->
    ok.


handle_read_error(ConfigDB, ReasonString, SocketType, Socket, Peername,
                  Item, Default) ->
    ?vlog("error reading request: ~s",[ReasonString]),
    E = lists:flatten(
          io_lib:format("Error reading request: ~s",[ReasonString])),
    error_log(mod_log, SocketType, Socket, ConfigDB, Peername, E),
    error_log(mod_disk_log, SocketType, Socket, ConfigDB, Peername, E),
    case httpd_util:lookup(ConfigDB,Item,Default) of
        reply414 ->
            send_read_status(SocketType, Socket, 414, ReasonString, ConfigDB);
        _ ->
            ok
    end.

send_read_status(SocketType, Socket, Code, ReasonString, ConfigDB) ->
    httpd_response:send_status(SocketType, Socket, Code, ReasonString,
			       ConfigDB).


error_log(Mod, SocketType, Socket, ConfigDB, Peername, String) ->
    Modules = httpd_util:lookup(ConfigDB, modules,
				[mod_get, mod_head, mod_log]),
    case lists:member(Mod, Modules) of
	true ->
	    Mod:error_log(SocketType, Socket, ConfigDB, Peername, String);
	_ ->
	    ok
    end.


sz(L) when list(L) ->
    length(L);
sz(B) when binary(B) ->
    size(B);
sz(O) ->
    {unknown_size,O}.


%% Socket utility functions:

close(SocketType, Socket, ConfigDB) ->
    case httpd_socket:close(SocketType, Socket) of
        ok ->
            ok;
        {error, Reason} ->
            ?vlog("error while closing socket: ~p",[Reason]),
	    ok
    end.

close_sleep({ssl, _}, Time) ->
    sleep(Time);
close_sleep(_, _) ->
    ok.


sleep(T) -> receive after T -> ok end.


dec(N) when integer(N) ->
    N-1;
dec(N) ->
    N.


content_length(#mod{parsed_header = Ph}) ->
    list_to_integer(httpd_util:key1search(Ph, "content-length","0")).


remove_newline(List)->
    lists:dropwhile(fun newline/1,List).

newline($\r) ->
    true;
newline($\n) ->
    true;
newline(_Sign) ->
    false.