Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
client_connection.ml1 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(*---------------------------------------------------------------------------- Copyright (c) 2017-2019 Inhabited Type LLC. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the author nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ----------------------------------------------------------------------------*) module Reader = Parse.Reader module Writer = Serialize.Writer module Oneshot = struct type error = [ `Malformed_response of string | `Invalid_response_body_length of Response.t | `Exn of exn ] type response_handler = Response.t -> [`read] Body.t -> unit type error_handler = error -> unit type state = | Awaiting_response | Received_response of Response.t * [`read] Body.t | Closed type t = { request : Request.t ; request_body : [ `write ] Body.t ; error_handler : (error -> unit) ; reader : Reader.response ; writer : Writer.t ; state : state ref ; mutable error_code : [ `Ok | error ] } let request ?(config=Config.default) request ~error_handler ~response_handler = let state = ref Awaiting_response in let request_method = request.Request.meth in let handler response body = state := Received_response(response, body); response_handler response body in let request_body = Body.create (Bigstringaf.create config.request_body_buffer_size) in let t = { request ; request_body ; error_handler ; error_code = `Ok ; reader = Reader.response ~request_method handler ; writer = Writer.create () ; state } in Writer.write_request t.writer request; request_body, t ;; let flush_request_body t = if Body.has_pending_output t.request_body then let encoding = match Request.body_length t.request with | `Fixed _ | `Chunked as encoding -> encoding | `Error _ -> assert false (* XXX(seliopou): This needs to be handled properly *) in Body.transfer_to_writer_with_encoding t.request_body ~encoding t.writer ;; let set_error_and_handle_without_shutdown t error = t.state := Closed; t.error_code <- (error :> [`Ok | error]); t.error_handler error; ;; let unexpected_eof t = set_error_and_handle_without_shutdown t (`Malformed_response "unexpected eof"); ;; let shutdown_reader t = Reader.force_close t.reader; begin match !(t.state) with | Awaiting_response -> unexpected_eof t; | Closed -> () | Received_response(_, response_body) -> Body.close_reader response_body; Body.execute_read response_body; end; ;; let shutdown_writer t = flush_request_body t; Writer.close t.writer; Body.close_writer t.request_body; ;; let shutdown t = shutdown_reader t; shutdown_writer t; ;; let set_error_and_handle t error = Reader.force_close t.reader; begin match !(t.state) with | Closed -> () | Awaiting_response -> set_error_and_handle_without_shutdown t error; | Received_response(_, response_body) -> Body.close_reader response_body; Body.execute_read response_body; set_error_and_handle_without_shutdown t error; end ;; let report_exn t exn = set_error_and_handle t (`Exn exn) ;; let flush_response_body t = match !(t.state) with | Awaiting_response | Closed -> () | Received_response(_, response_body) -> try Body.execute_read response_body with exn -> report_exn t exn ;; let _next_read_operation t = match !(t.state) with | Awaiting_response | Closed -> Reader.next t.reader | Received_response(_, response_body) -> if not (Body.is_closed response_body) then Reader.next t.reader else begin Reader.force_close t.reader; Reader.next t.reader end ;; let next_read_operation t = match _next_read_operation t with | `Error (`Parse(marks, message)) -> let message = String.concat "" [ String.concat ">" marks; ": "; message] in set_error_and_handle t (`Malformed_response message); `Close | `Error (`Invalid_response_body_length _ as error) -> set_error_and_handle t error; `Close | (`Read | `Close) as operation -> operation ;; let read_with_more t bs ~off ~len more = let consumed = Reader.read_with_more t.reader bs ~off ~len more in flush_response_body t; consumed ;; let read t bs ~off ~len = read_with_more t bs ~off ~len Incomplete let read_eof t bs ~off ~len = let bytes_read = read_with_more t bs ~off ~len Complete in begin match !(t.state) with | Received_response _ | Closed -> () | Awaiting_response -> unexpected_eof t; end; bytes_read ;; let next_write_operation t = flush_request_body t; if Body.is_closed t.request_body then Writer.close t.writer; Writer.next t.writer ;; let yield_writer t k = if Body.is_closed t.request_body then begin Writer.close t.writer; k () end else Body.when_ready_to_write t.request_body k let report_write_result t result = Writer.report_result t.writer result let is_closed t = Reader.is_closed t.reader && Writer.is_closed t.writer end