diff --git a/src/mte.ml b/src/mte.ml index 49d5cdd4..483e14ae 100644 --- a/src/mte.ml +++ b/src/mte.ml @@ -15,27 +15,32 @@ type response = { content: string; } +let request_route request = + match String.split_on_char '/' request.target with + | "" :: l -> l + | _ -> assert false + +let content_type_to_string = function + | `Text_plain -> "text/plain" + | `Application_json -> "application/json" + +let default_headers ~content typ = + Headers.of_list + [ + ("content-length", string_of_int (String.length content)); + ("connection", "close"); ("content-type", content_type_to_string typ); + ] + let home_page_text = "Hello!\n\nThis is MTE, the MirageOS Taler Exchange unikernel." let request_handler request = - match String.split_on_char '/' request.target with - | [ ""; "" ] -> - let headers = - Headers.of_list - [ - ("content-length", string_of_int (String.length home_page_text)); - ("connection", "close"); ("content-type", "text/plain"); - ] - in - { status= `OK; headers; content= home_page_text } + match request_route request with + | [ "" ] -> + let content = home_page_text in + let headers = default_headers ~content `Text_plain in + { status= `OK; headers; content } | _ -> let content = "Not found." in - let headers = - Headers.of_list - [ - ("content-length", string_of_int (String.length content)); - ("connection", "close"); ("content-type", "text/plain"); - ] - in + let headers = default_headers ~content `Text_plain in { status= `Not_found; headers; content }