Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions cohttp/code.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
open Sexplib.Std
type version = [ `HTTP_1_0 | `HTTP_1_1 ] with sexp

type meth = [ `GET | `POST | `HEAD | `DELETE | `PATCH | `PUT | `OPTIONS ] with sexp
type standard_meth = [ `GET | `POST | `HEAD | `DELETE | `PATCH | `PUT | `OPTIONS ] with sexp

type meth = [ standard_meth | `Other of string ] with sexp

type informational_status =
[ `Continue
Expand Down Expand Up @@ -116,16 +118,20 @@ let string_of_method: meth -> string = function
| `PATCH -> "PATCH"
| `PUT -> "PUT"
| `OPTIONS -> "OPTIONS"
| `Other s -> s

let method_of_string: string -> meth option = function
| "GET" -> Some `GET
| "POST" -> Some `POST
| "HEAD" -> Some `HEAD
| "DELETE" -> Some `DELETE
| "PATCH" -> Some `PATCH
| "PUT" -> Some `PUT
| "OPTIONS" -> Some `OPTIONS
| _ -> None
let method_of_string: string -> meth = function
| "GET" -> `GET
| "POST" -> `POST
| "HEAD" -> `HEAD
| "DELETE" -> `DELETE
| "PATCH" -> `PATCH
| "PUT" -> `PUT
| "OPTIONS" -> `OPTIONS
| _ as s -> `Other s

let compare_method m1 m2 =
String.compare (string_of_method m1) (string_of_method m2)

let status_of_code: int -> status_code = function
| 100 -> `Continue
Expand Down
9 changes: 7 additions & 2 deletions cohttp/code.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
open Sexplib.Std
type version = [ `HTTP_1_0 | `HTTP_1_1 ] with sexp

type meth = [ `GET | `POST | `HEAD | `DELETE | `PATCH | `PUT | `OPTIONS ] with sexp
type standard_meth = [ `GET | `POST | `HEAD | `DELETE | `PATCH | `PUT | `OPTIONS ] with sexp

type meth = [ standard_meth | `Other of string ] with sexp

type informational_status =
[ `Continue (** Client should continue with request *)
Expand Down Expand Up @@ -114,9 +116,12 @@ val version_of_string: string -> version option
val string_of_method: meth -> string
(** Convert a method to a string. *)

val method_of_string: string -> meth option
val method_of_string: string -> meth
(** Convert a string to a method. Return [None] if the conversion fails. *)

val compare_method : meth -> meth -> int
(** Compare two methods by name. *)


val status_of_code: int -> status_code
(** Generate status values from int codes. *)
Expand Down
9 changes: 4 additions & 5 deletions cohttp/request.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ module Make(IO : S.IO) = struct
| Some request_line -> begin
match Stringext.split request_line ~on:' ' with
| [ meth_raw; path; http_ver_raw ] -> begin
match method_of_string meth_raw, version_of_string http_ver_raw with
| Some m, Some v -> return (`Ok (m, path, v))
| None, Some v -> return (`Invalid ("Malformed request method: " ^ meth_raw))
| Some v, None -> return (`Invalid ("Malformed request HTTP version: " ^ http_ver_raw))
| None, None -> return (`Invalid ("Malformed request method and version: " ^ request_line))
let m = method_of_string meth_raw in
match version_of_string http_ver_raw with
| Some v -> return (`Ok (m, path, v))
| None -> return (`Invalid ("Malformed request HTTP version: " ^ http_ver_raw))
end
| _ -> return (`Invalid ("Malformed request header: " ^ request_line))
end
Expand Down