package simple_httpd

  1. Overview
  2. Docs

Source file Output.ml

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

type t = { fd : Async.client; b: Bytes.t
           ; mutable o : int; s : int }

let create ?(buf_size=16* 4_096) fd =
  {fd; s=buf_size; o = 0; b=Bytes.make buf_size ' '}

let sock oc = oc.fd.sock

let push oc =
  assert(oc.o = oc.s);
  let w = Async.Client.write oc.fd oc.b 0 oc.s in
  if w < oc.s then
    begin
      Bytes.blit oc.b w oc.b 0 (oc.s - w);
      oc.o <- oc.s - w
    end
  else
    oc.o <- 0

let flush oc =
  let n = ref 0 in
  while !n < oc.o do
    let w = Async.Client.write oc.fd oc.b !n (oc.o - !n) in
    n := !n + w
  done;
  oc.o <- 0

let close oc =
  flush oc; Async.Client.close oc.fd

let add_substring oc str offset len =
  if oc.o + len <= oc.s then
    begin
      Bytes.blit_string str offset oc.b oc.o len;
      oc.o <- oc.o + len
    end
  else
    begin
      let start, remain =
        if oc.o > 0 then
          begin
            let nb = oc.s - oc.o in
            Bytes.blit_string str offset oc.b oc.o nb;
            oc.o <- oc.s;
            flush oc;
            offset + nb, len - nb
          end
        else
          offset, len
      in
      let n = ref start in
      let r = ref remain in
      while !r > oc.s do
        let str = Bytes.unsafe_of_string str in
        let w = Async.Client.write oc.fd str !n !r in
        n := !n + w;
        r := !r - w;
      done;
      Bytes.blit_string str !n oc.b 0 !r;
      oc.o <- !r
    end

let add_string oc str = add_substring oc str 0 (String.length str)

let add_bytes oc str =
  add_string oc (Bytes.unsafe_to_string str)

let add_subbytes oc str offset len =
  add_substring oc (Bytes.unsafe_to_string str) offset len

let add_char oc c =
  if oc.o >= oc.s then push oc;
  Bytes.set oc.b oc.o c;
  oc.o <- oc.o + 1

let add_decimal oc n =
  let b = ref 1 in
  while n / !b >= 10 do
    b := 10 * !b
  done;
  let n = ref n in
  while !b > 0 do
    let d = (!n / !b) mod 10 in
    let c = Char.chr (d + Char.code '0') in
    add_char oc c;
    n := !n mod !b;
    b := !b / 10
  done

let add_hexa oc n =
  let b = ref 0 in
  while n lsr !b > 0xf do
    b := !b + 4
  done;
  while !b >= 0 do
    let d = (n lsr !b) land 0xf in
    let c = if d < 10 then Char.chr (d + Char.code '0')
            else Char.chr (d - 10 + Char.code 'a')
    in
    add_char oc c;
    b := !b - 4
  done

let printf oc format =
  let cont s = add_string oc s in
  Printf.ksprintf cont format
(*
  let free_space oc =
  let r = oc.s - oc.o in
  if r = 0 then (flush oc; oc.s) else r
 *)

(* print a stream as a series of chunks: no allocation! *)
let output_chunked ?synch (oc:t) (self:Input.t) : unit =
  let continue = ref true in
  while !continue do
    (* next chunk *)
    self.fill_buf();
    let n = self.len in
    add_hexa oc n;
    add_string oc "\r\n";
    add_subbytes oc self.bs self.off n;
    add_string oc "\r\n";
    self.consume n;
    if n = 0 then (
      continue := false
    ) else (match synch with
           | None -> ()
           | Some f -> flush oc; f ())
  done;
  (*add_string oc "\r\n";*) (* empty trailer required by RFC *)
  ()

let output_raw ?synch (oc:t) (self:Input.t) len : unit =
  let len = ref len in
  try
    while !len > 0 do
      self.fill_buf();
      let n = self.len in
      add_subbytes oc self.bs self.off (min n !len);
      len := !len - n;
      self.consume n;
      if !len > 0 then
        (match synch with
         | None -> ()
         | Some f -> flush oc; f ())
    done;
    ()
  with e ->
        Log.f (Exc 0) (fun k -> k "output_raw exc: %s" (Printexc.to_string e));
        raise e

let output_str = add_string
let output_bytes = add_bytes

let sendfile oc n fd =
  let r = ref n in
  if oc.s > oc.o then
    begin
      let w = Util.read fd oc.b oc.o (min (oc.s - oc.o) !r) in
      oc.o <- oc.o + w;
      r := !r - w;
    end;
  flush oc;
  while !r > 0  do
    let w = Async.Client.sendfile oc.fd fd (n - !r) !r in
    r := !r - w
  done