Advertisement
WizardofBair

Untitled

Mar 25th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. (*type statusCodes = OK | CREATED | ... ;;*)
  2.  
  3. (* HTTP/1.1 200 OK *)
  4.  
  5. type status = {version : string; code : int} ;;
  6. let string_of_status status = status.version ^ " " ^ string_of_int status.code ^ " " ^ match status.code with | 0 -> "GH" | 100 -> "Continue" | 101 -> "Switching Protocol" | 200 -> "OK" | 201 -> "CREATED" | 404 -> "NOT FOUND" | 418 -> "I AM A TEAPOT" | n -> "Unknown Status " ^ string_of_int n;;
  7.  
  8. type field = | Server of string | ContentLength of int | ContentType of string
  9. let string_of_field f = match f with
  10. | Server name -> "Server :" ^ name
  11. | ContentLength length -> "ContentLength :" ^ string_of_int length
  12. | ContentType ct -> "Content-Type: " ^ ct;;
  13. let sI = string_of_int ;;
  14. type date = {day : int; day_of_month : int ; month : int; year : int ; hour : int ; minute : int ; second : int ; time_zone : string};;
  15.  
  16. let string_of_date d = sI d.day ^ " " ^ sI d.day_of_month ^ " " ^ sI d.month ^ " " ^ sI d.year ^ " " ^ sI d.hour ^ " " ^ sI d.minute ^ " " ^ sI d.second ^ " " ^ d.time_zone ;;
  17.  
  18. type response = {
  19. status : status;
  20. date : date;
  21. headers : field list;
  22. expires : date;
  23. body : string;
  24. }
  25.  
  26. let mystatus = {version = "HTTP/1.1"; code = 200} ;;
  27.  
  28. let myresponse = {
  29. status = mystatus;
  30. headers = [
  31. Server "nginx/1.6.2";
  32. ContentLength 12;
  33. ContentType "text/html; charset=utf-8";
  34. ];
  35. expires = {19;12;1978;5;0;0;"GMT"};
  36. body = "hello world!";
  37. }
  38.  
  39. (*MAP function called to all members
  40. List.map string_of_int stevila ;;
  41. String.concat "" (List.map string_of_int stevila); *)
  42.  
  43. let string_of_response r = string_of_status r.status ^ "\r\n" ^ String.concat "\r\n" (List.map string_of_field r.headers) ^ "\r\n" ^ string_of_date d.expires ^ "\r\n" ^ r.body ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement