2 * framepp.ml Copyright (C) 2005 Denis Berthod
3 * Laboratoire PPS - CNRS Université Paris Diderot
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, with linking exception;
8 * either version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 (** pretty printer for http frames*)
21 open Ocsigen_http_frame
23 module H
= Http_header
25 (** converts the method into a string*)
26 let string_of_method =
32 | H.DELETE
-> "DELETE"
34 | H.OPTIONS
-> "OPTIONS"
35 | H.CONNECT
-> "CONNECT"
37 | H.UNLINK
-> "UNLINK"
40 (** converts a string to a method *)
41 let method_of_string =
47 | "DELETE" -> H.DELETE
49 | "OPTIONS" -> H.OPTIONS
50 | "CONNECT" -> H.CONNECT
52 | "UNLINK" -> H.UNLINK
54 | _
-> failwith
"method_of_string"
56 (** converts the protocol into a string *)
57 let string_of_proto = function
58 | H.HTTP10
-> "HTTP/1.0"
59 | H.HTTP11
-> "HTTP/1.1"
61 (** converts a string to a protocol *)
62 let proto_of_string = function
63 | "HTTP/1.0" -> H.HTTP10
64 | "HTTP/1.1" -> H.HTTP11
65 | _
-> failwith
"proto_of_string"
67 (** Write the first line of an HTTP frame to a string buffer *)
68 let fst_line buf header
=
69 match header
.H.mode
with
72 Printf.bprintf buf
"%s %i %s\r\n" (string_of_proto header
.H.proto
)
73 code
(Http_error.expl_of_code code
)
74 | H.Query
(meth
, url
) ->
75 Printf.bprintf buf
"%s %s %s\r\n"
76 (string_of_method meth
) url
(string_of_proto header
.H.proto
)
79 (** Prints the content of a header. To prevent http header injection,
80 we insert spaces (' ') after CRLF, in case the user has not done this
81 himself. Also, if we find single CR or LF, we replace them by spaces .
82 (This is correct according to the RFC, as the headers content should not
83 contain single CR or LF anyway) *)
84 let print_header_content buf content
=
85 let s = String.length content
in
89 Buffer.add_substring buf content prev
(s-prev
))
91 let add_prev () = Buffer.add_substring buf content prev
(i
-prev
) in
92 match content
.[i
] with
97 Buffer.add_char buf c
;
98 Buffer.add_char buf ' '
;
102 (match content
.[i'
] with
103 | '
\n'
| '
\r'
as c'
when c
<> c'
->
105 Buffer.add_char buf c
; Buffer.add_char buf c'
;
106 Buffer.add_char buf ' '
;
120 let b = Buffer.create
0 in print_header_content b s; Buffer.contents
b
123 (** Write the header lines to a string buffer *)
124 let headers buf header
=
127 Printf.bprintf buf
"%s: %a\r\n"
128 (Http_headers.name_to_string name
) print_header_content value)
131 (** Convert a HTTP header into a string *)
132 let string_of_header hds
=
133 let buf = Buffer.create
200 in
136 Printf.bprintf
buf "\r\n%!";