# HG changeset patch # User Phil Pennock # Date 1334995596 25200 # Node ID 136777f56c79d257eec30ce603cc91cbaaaf08fd # Parent a2e2778cb7f7d5bc39282855b2ef58f865e111f0 Limit fix for limit=0 Return real status text strings, rather than confusing "500 OK". Handle No_results as an exception type, giving 404 instead of 500. Treat limit of -1 (or <0) as "unlimited". Handle limit=0 so that can ask for number of results without getting results. diff --git a/dbserver.ml b/dbserver.ml --- a/dbserver.ml +++ b/dbserver.ml @@ -206,7 +206,7 @@ struct in tsort_keys keys in - if keys = [] then raise (Wserver.Misc_error "No keys found") + if keys = [] then raise (Wserver.No_results "No keys found") else keys @@ -220,7 +220,7 @@ struct then result else (trunc_c (result @ [h]) tail (num-1)) in - if count > 0 + if count >= 0 then trunc_c [] keys count else keys @@ -236,8 +236,12 @@ struct let keys = clean_keys request keys in let count = List.length keys in let keys = truncate request.limit keys in - let keystr = Key.to_string_multiple keys in - let aakeys = Armor.encode_pubkey_string keystr in + let aakeys = + match keys with + | [] -> "" + | _ -> let keystr = Key.to_string_multiple keys in + Armor.encode_pubkey_string keystr + in ("text/html; charset=UTF-8", count, HtmlTemplates.page diff --git a/key.ml b/key.ml --- a/key.ml +++ b/key.ml @@ -140,7 +140,7 @@ let of_string_multiple keystr = in loop () -let to_string_multiple keys = +let to_string_multiple keys = let cout = Channel.new_buffer_outc 0 in List.iter ~f:(fun key -> write key cout) keys; cout#contents diff --git a/request.ml b/request.ml --- a/request.ml +++ b/request.ml @@ -49,7 +49,7 @@ let default_request = { kind = Index; exact = false; machine_readable = false; clean = true; - limit = 0; + limit = (-1); } let comma_rxp = Str.regexp "," diff --git a/wserver.ml b/wserver.ml --- a/wserver.ml +++ b/wserver.ml @@ -27,8 +27,9 @@ open Unix module Map = PMap.Map module Set = PSet.Set +exception Misc_error of string +exception No_results of string exception Not_implemented of string -exception Misc_error of string exception Page_not_found of string let ( |= ) map key = Map.find key map @@ -237,7 +238,16 @@ let request_to_string_short request = let send_result cout ?(error_code = 200) ?(content_type = "text/html; charset=UTF-8") ?(count = -1) body = - fprintf cout "HTTP/1.0 %03d OK\r\n" error_code; + let text_status = + match error_code with + | 200 -> "OK" + | 404 -> "Not Found" + | 408 -> "Request Timeout" + | 500 -> "Internal Server Error" + | 501 -> "Not Implemented" + | _ -> "???" + in + fprintf cout "HTTP/1.0 %03d %s\r\n" error_code text_status; fprintf cout "Server: sks_www/%s\r\n" version; fprintf cout "Content-length: %u\r\n" (String.length body + 2); if count >= 0 then @@ -291,6 +301,15 @@ let accept_connection f ~recover_timeout in send_result cout ~error_code:404 output + | No_results s -> + ignore (Unix.alarm recover_timeout); + plerror 2 "No results for request %s: %s" + (request_to_string request) s; + let output = HtmlTemplates.page ~title:"No results found" + ~body:(sprintf "No results found: %s" s) + in + send_result cout ~error_code:404 output + | Misc_error s -> ignore (Unix.alarm recover_timeout); plerror 2 "Error handling request %s: %s"