@@ -106,14 +106,18 @@ Options:
106106 (resp/content-type mime))))
107107
108108(defn create-resp [scgi-response]
109- (let [resp-array (str/split scgi-response #"\r\n " )
110- resp-status (first resp-array)
111- end (+ 4 (str/index-of scgi-response " \r\n\r\n " ))
112- status (Integer/parseInt (if (empty? resp-status) " 404" resp-status))
113- body (subs scgi-response end)
114- header-vals (re-seq #"(.*?): (.*?)\r\n " (subs scgi-response 0 end))
115- headers (apply merge (for [h header-vals] {(nth h 1 ) (nth h 2 )}))]
116- {:status status :body body :headers headers}))
109+ (when-not (str/blank? scgi-response)
110+ (let [resp-array (str/split scgi-response #"\r\n " )
111+ resp-status (first resp-array)
112+ status (Integer/parseInt (if (empty? resp-status) " 404" resp-status))
113+ body (last resp-array )
114+ header-vals' (-> scgi-response
115+ (str/replace (first resp-array) " " )
116+ (str/replace (last resp-array) " " )
117+ (str/trim ))
118+ header-vals (re-seq #"(.*?): (.*?)\r\n " header-vals')
119+ headers (apply merge (for [h header-vals] {(nth h 1 ) (nth h 2 )}))]
120+ {:status status :body body :headers headers})))
117121
118122(defn file-exists? [path]
119123 (-> path ^File io/file .exists))
@@ -140,10 +144,9 @@ Options:
140144 (-> (assoc full :document-uri " /404.clj" ) http-to-scgi (forward (:scgi-port opts)) create-resp)
141145 :else (format-response 404 nil nil )))))
142146
143- (defn run-file [path port]
147+ (defn run-file [path scgi- port]
144148 (let [path (str/replace (str " /" path) " //" " /" )
145149 root (.getCanonicalPath ^File (io/file " ./" ))
146- scgi-port (Integer/parseInt (or (System/getenv " SCGI_PORT" ) (str port) " 9000" ))
147150 request {:document-root root :document-uri path :request-method :get }]
148151 (-> request http-to-scgi (forward scgi-port) create-resp :body )))
149152
@@ -259,21 +262,6 @@ Options:
259262 (slurp (str (template-path ) " /api/info.clj" )))
260263 (println (str " Created pcp project `" project-name " ` in directory" ) (.getAbsolutePath ^File (io/file path)))))
261264
262- ; ; (def cli-options
263- ; ; ;; An option with a required argument
264- ; ; [["-s" "--serve PATH" "Server root"
265- ; ; :default "./public"
266- ; ; :parse-fn #(Integer/parseInt %)
267- ; ; :validate [#(< 0 % 0x10000) "Must be a number between 0 and 65536"]]
268- ; ; ["-t" nil "SCGI server port"
269- ; ; :default 9000
270- ; ; :validate [#(< 0 % 0x10000) "Must be a number between 0 and 65536"]]
271- ; ; ["-v" "--version"]
272- ; ; ["-h" "--help"]])
273-
274- ; ; (defn -main [& args]
275- ; ; (println (parse-opts args cli-options)))
276-
277265(defn -main
278266 ([]
279267 (-main " " " " ))
@@ -285,8 +273,8 @@ Options:
285273 " --serve" (start-local-server {:root value})
286274 " -v" (println " pcp" version)
287275 " --version" (println " pcp" version)
288- " -e" (println (run-file value 9000 ))
289- " --evaluate" (println (run-file value 9000 ))
276+ " -e" (println (run-file value ( Integer/parseInt ( or ( System/getenv " SCGI_PORT " ) " 9000" )) ))
277+ " --evaluate" (println (run-file value ( Integer/parseInt ( or ( System/getenv " SCGI_PORT " ) " 9000" )) ))
290278 " new" (new-project value)
291279 " passphrase" (add-passphrase value)
292280 " secret" (add-secret {:root value})
0 commit comments