package gapi-ocaml
A simple OCaml client for Google Services
Install
Dune Dependency
Authors
Maintainers
Sources
v0.4.6.tar.gz
sha256=b84b680528a5e050014103a8e7a60a5d43efd5fefc3f838310bd46769775ab48
md5=8ee26acf1f6c6f5e24c7b57fa070a0a2
doc/src/gapi-ocaml.netstring-local/netmime.ml.html
Source file netmime.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 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
(* $Id$ * ---------------------------------------------------------------------- * *) open Netchannels type store = [ `Memory | `File of string ] exception Immutable of string class type mime_header_ro = object method fields : (string * string) list method field : string -> string method multiple_field : string -> string list end class type mime_header = object inherit mime_header_ro method ro : bool method set_fields : (string * string) list -> unit method update_field : string -> string -> unit method update_multiple_field : string -> string list -> unit method delete_field : string -> unit end class type mime_body_ro = object method value : string method store : store method open_value_rd : unit -> in_obj_channel method finalize : unit -> unit end class type mime_body = object inherit mime_body_ro method ro : bool method set_value : string -> unit method open_value_wr : unit -> out_obj_channel end type complex_mime_message = mime_header * complex_mime_body and complex_mime_body = [ `Body of mime_body | `Parts of complex_mime_message list ] type complex_mime_message_ro = mime_header_ro * complex_mime_body_ro and complex_mime_body_ro = [ `Body of mime_body_ro | `Parts of complex_mime_message_ro list ] (* Check that coercion is possible: *) let _ = fun x -> (x : complex_mime_message :> complex_mime_message_ro) type mime_message = mime_header * [ `Body of mime_body ] type mime_message_ro = mime_header_ro * [ `Body of mime_body_ro ] module CI : sig (* case-insensitive strings *) type t val compare : t -> t -> int val make : string -> t end = struct type t = string let compare (a_ci:t) (b_ci:t) = compare a_ci b_ci let make s = STRING_LOWERCASE s end module CIMap = Map.Make(CI) (* Maps from case-insensitive strings to any type *) module DL : sig (* doubly-linked lists *) type 'a t type 'a cell val create : unit -> 'a t val is_empty : 'a t -> bool val cell : 'a -> 'a cell val contents : 'a cell -> 'a val first : 'a t -> 'a cell (* or Not_found *) val last : 'a t -> 'a cell (* or Not_found *) val prev : 'a cell -> 'a cell (* or Not_found *) val next : 'a cell -> 'a cell (* or Not_found *) val iter : ('a cell -> unit) -> 'a t -> unit val delete : 'a cell -> unit val insert_after : neo:'a cell -> 'a cell -> unit val add_at_end : neo:'a cell -> 'a t -> unit val replace : neo:'a cell -> 'a cell -> unit val of_list : 'a list -> 'a t val to_list : 'a t -> 'a list end = struct type 'a t = { mutable first : 'a cell option; mutable last : 'a cell option; } and 'a cell = { mutable prev : 'a cell option; mutable next : 'a cell option; mutable list : 'a t option; contents : 'a; } let create() = { first = None; last = None } let is_empty l = l.first = None let cell x = { prev = None; next = None; list = None; contents = x } let contents c = c.contents let first l = match l.first with Some c -> c | None -> raise Not_found let last l = match l.last with Some c -> c | None -> raise Not_found let prev c = match c.prev with Some c' -> c' | None -> raise Not_found let next c = match c.next with Some c' -> c' | None -> raise Not_found let iter f l = match l.first with Some c -> f c; let current = ref c in while (let c0 = ! current in c0.next) <> None do (* Error in camlp4 *) current := next !current; f !current done; () | None -> () let delete c = match c.list with Some l -> ( match c.prev with Some p -> p.next <- c.next | None -> l.first <- c.next ); ( match c.next with Some n -> n.prev <- c.prev | None -> l.last <- c.prev ); c.prev <- None; c.next <- None; c.list <- None | None -> failwith "DL.delete: cannot delete free cell" let insert_after ~neo c = if neo.list <> None then failwith "DL.insert_after: new cell must be free"; match c.list with Some l -> let nx = c.next in c.next <- Some neo; neo.prev <- Some c; ( match nx with Some n -> n.prev <- Some neo; neo.next <- Some n; | None -> l.last <- Some neo; neo.next <- None ); neo.list <- Some l | None -> failwith "DL.insert_after: cannot insert after free cell" let add_at_end ~neo l = if neo.list <> None then failwith "DL.insert_after: new cell must be free"; match l.last with Some n -> n.next <- Some neo; neo.prev <- Some n; neo.next <- None; neo.list <- Some l; l.last <- Some neo | None -> l.last <- Some neo; l.first <- Some neo; neo.prev <- None; neo.next <- None; neo.list <- Some l let replace ~neo c = if neo.list <> None then failwith "DL.replace: new cell must be free"; match c.list with Some l -> ( match c.prev with Some p -> p.next <- Some neo | None -> l.first <- Some neo ); neo.prev <- c.prev; ( match c.next with Some n -> n.prev <- Some neo | None -> l.last <- Some neo ); neo.next <- c.next; neo.list <- Some l; c.prev <- None; c.next <- None; c.list <- None | None -> failwith "DL.replace: cannot replace free cell" let of_list l = let dl = create() in List.iter (fun x -> add_at_end ~neo:(cell x) dl ) l; dl let rec to_list dl = chain_to_list dl.first and chain_to_list chain = match chain with None -> [] | Some c -> c.contents :: chain_to_list c.next end class basic_mime_header h : mime_header = object (self) val mutable hdr_map = lazy (assert false) val mutable hdr_dl = lazy (assert false) initializer self # do_set_fields h method ro = false (* Heirs can redefine [ro] to make this object immutable *) method fields = DL.to_list (Lazy.force hdr_dl) method field n = let m = Lazy.force hdr_map in match CIMap.find (CI.make n) m with [] -> raise Not_found | cell :: _ -> snd (DL.contents cell) method multiple_field n = let m = Lazy.force hdr_map in try List.map (fun cell -> snd (DL.contents cell)) (CIMap.find (CI.make n) m) with Not_found -> [] method set_fields h = if self#ro then raise (Immutable "set_fields"); self # do_set_fields h method private do_set_fields h = hdr_dl <- lazy (DL.of_list h); hdr_map <- lazy begin (* This seems to be expensive (O(n log n)). Because of this we do it only * on demand; maybe nobody accesses the header at all *) let m = ref CIMap.empty in DL.iter (fun cell -> let (n,v) = DL.contents cell in let n_ci = CI.make n in let current = try CIMap.find n_ci !m with Not_found -> [] in m := CIMap.add n_ci (cell :: current) !m; ) (Lazy.force hdr_dl); CIMap.map List.rev !m end method update_field n v = if self#ro then raise (Immutable "update_field"); self # update_multiple_field n [v] method update_multiple_field n vl = if self#ro then raise (Immutable "update_multiple_field"); let n_ci = CI.make n in let m = Lazy.force hdr_map in let dl = Lazy.force hdr_dl in (* Algorithm: First try to replace existing values. * If there are more new values than old values, * at the excess values after the last old value, * or if not possible, at the end. *) let insert_point = ref None in let old_cells = ref(try CIMap.find n_ci m with Not_found -> []) in let new_vals = ref vl in let new_cells = ref [] in while !old_cells <> [] || !new_vals <> [] do match !old_cells, !new_vals with (old_cell :: old_cells'), (new_val :: new_vals') -> (* Only update if the value has changed: *) let (old_n, old_val) = DL.contents old_cell in if old_val = new_val then ( new_cells := old_cell :: !new_cells; insert_point := Some old_cell; ) else ( let new_cell = DL.cell (n, new_val) in DL.replace ~neo:new_cell old_cell; insert_point := Some new_cell; new_cells := new_cell :: !new_cells ); old_cells := old_cells'; new_vals := new_vals'; | [], (new_val :: new_vals') -> let new_cell = DL.cell (n, new_val) in ( match !insert_point with Some p -> DL.insert_after ~neo:new_cell p; | None -> DL.add_at_end ~neo:new_cell dl ); new_vals := new_vals'; insert_point := Some new_cell; new_cells := new_cell :: !new_cells | (old_cell :: old_cells'), [] -> DL.delete old_cell; old_cells := old_cells' | [], [] -> assert false done; let m' = CIMap.add n_ci (List.rev !new_cells) m in hdr_map <- lazy m' method delete_field n = if self#ro then raise (Immutable "delete_field"); let n_ci = CI.make n in let m = Lazy.force hdr_map in let old_cells = try CIMap.find n_ci m with Not_found -> [] in List.iter DL.delete old_cells; let m' = CIMap.remove n_ci m in hdr_map <- lazy m'; end ;; let basic_mime_header = new basic_mime_header class wrap_mime_header hdr : mime_header = object(self) method fields = hdr#fields method field = hdr#field method multiple_field = hdr#multiple_field method ro = hdr#ro (* Heirs can redefine [ro] to make this object immutable *) method set_fields fields = if self#ro then raise(Immutable "set_fields"); hdr#set_fields fields method update_field n v = if self#ro then raise(Immutable "update_field"); hdr#update_field n v method update_multiple_field n v = if self#ro then raise(Immutable "update_multiple_fields"); hdr#update_multiple_field n v method delete_field n = if self#ro then raise(Immutable "delete_field"); hdr#delete_field n end class wrap_mime_header_ro hdr : mime_header = object(self) method fields = hdr#fields method field = hdr#field method multiple_field = hdr#multiple_field method ro = true method set_fields _ = raise (Immutable "set_fields") method update_field _ _ = raise (Immutable "update_field") method update_multiple_field _ _ = raise (Immutable "update_multiple_field") method delete_field _ = raise (Immutable "delete_field") end let wrap_mime_header_ro = new wrap_mime_header_ro class memory_mime_body v : mime_body = object (self) val mutable value = v val mutable finalized = false method value = if finalized then self # finalized(); value method store = `Memory method open_value_rd() = if finalized then self # finalized(); new input_string value method finalize() = finalized <- true method ro = (* Heirs can redefine [ro] to make this object immutable *) false method set_value s = if self#ro then raise (Immutable "set_value"); if finalized then self # finalized(); value <- s; method open_value_wr() = if self#ro then raise (Immutable "open_value_wr"); if finalized then self # finalized(); let b = Netbuffer.create 60 in new output_netbuffer ~onclose:(fun () -> value <- Netbuffer.contents b) b; method private finalized() = failwith "Netmime.memory_mime_body: object is finalized"; end ;; let memory_mime_body = new memory_mime_body class file_mime_body ?(fin=false) f : mime_body = object (self) val mutable finalized = false val fin = fin val filename = f val cached_value = Weak.create 1 method ro = (* Heirs can redefine [ro] to make this object immutable *) false method store = `File filename method value = if finalized then self # finalized(); match Weak.get cached_value 0 with None -> with_in_obj_channel (new input_channel (open_in_bin filename)) (fun objch -> let v = string_of_in_obj_channel objch in Weak.set cached_value 0 (Some v); v ) | Some v -> v method open_value_rd() = if finalized then self # finalized(); new input_channel (open_in_bin filename) method set_value s = if self#ro then raise (Immutable "set_value"); if finalized then self # finalized(); with_out_obj_channel (new output_channel (open_out_bin filename)) (fun ch -> ch # output_string s); method open_value_wr() = if self#ro then raise (Immutable "open_value_wr"); if finalized then self # finalized(); new output_channel (open_out_bin filename) method finalize () = if fin && not finalized then begin try Sys.remove filename with _ -> () end; finalized <- true method private finalized() = failwith "Netmime.file_mime_body: object is finalized"; end ;; let file_mime_body = new file_mime_body class wrap_mime_body bdy : mime_body = object (self) method value = bdy#value method store = bdy#store method open_value_rd = bdy#open_value_rd method finalize = bdy#finalize method ro = bdy#ro method set_value = bdy#set_value method open_value_wr = bdy#open_value_wr end class wrap_mime_body_ro bdy : mime_body = object (self) method value = bdy#value method store = bdy#store method open_value_rd = bdy#open_value_rd method finalize = bdy#finalize method ro = true method set_value _ = raise (Immutable "set_value") method open_value_wr _ = raise (Immutable "open_value_wr") end let wrap_mime_body_ro = new wrap_mime_body_ro let rec wrap_complex_mime_message_ro (h,cb) = (wrap_mime_header_ro h, match cb with `Body b -> `Body(wrap_mime_body_ro b) | `Parts p -> `Parts(List.map wrap_complex_mime_message_ro p) ) ;;
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>