guile-pastebin/modules/pastebin/httpserver.scm

httpserver.scm

1
;;; SPDX-FileCopyrightText: 2021 Li Ian-Xue (b4283) <b4283@pm.me>
2
;;; SPDX-FileCopyrightText: 2025 Evgeny Pisemsky <mail@pisemsky.site>
3
;;;
4
;;; SPDX-License-Identifier: GPL-3.0-only
5
6
(define-module (pastebin httpserver))
7
8
(use-modules (web request)
9
             (web response)
10
             (web uri)
11
             (sxml simple)
12
             (pastebin data)
13
             (rnrs bytevectors)
14
             (ice-9 textual-ports)
15
             (ice-9 binary-ports)
16
             (ice-9 regex)
17
             (ice-9 match)
18
             (srfi srfi-1))
19
20
(export make-pastebin-handler)
21
22
(define (read-parts reqbody boundary)
23
  (define b2 (string-append "(\r\n)?--" boundary))
24
  (let A ((start 0) (parts '()))
25
    (let ((sm (string-match b2 reqbody start)))
26
      (if sm
27
          ;; +2 => CRLF
28
          (A (+ 2 (match:end sm 0))
29
             (cons (substring reqbody start (match:start sm 0)) parts))
30
          (cdr (reverse parts))))))
31
32
(define (get-new-pin str pin)
33
  (let ((crlfi (string-contains str "\r\n" pin)))
34
    (if crlfi crlfi (string-length str))))
35
36
(define (parse-part partstr)
37
  (let A ((headers '()) (pin 0))
38
    (let* ((newpin (get-new-pin partstr pin))
39
           (line (substring partstr pin newpin)))
40
      (if (string-null? line)
41
          (cons (reverse headers) (substring partstr (+ 2 newpin)))
42
          (A (cons line headers) (+ 2 newpin))))))
43
44
(define (get-content-dispo-name-from-headers headers)
45
  (let ((fl (find (lambda (line) (string-prefix-ci? "content-disposition: " line)) headers)))
46
    (if fl
47
        (let ((sm (string-match "name=(.*)" fl)))
48
          (if sm (string-trim-both (match:substring sm 1) #\") ""))
49
        "")))
50
51
(define (read-multipart-form-data reqbody boundary)
52
  (define parts (read-parts reqbody boundary))
53
  (map
54
   (lambda (part)
55
     (let ((pp (parse-part part)))
56
       (cons (get-content-dispo-name-from-headers (car pp)) (cdr pp))))
57
   parts))
58
59
(define (templatize title body)
60
  `(html (head
61
          (title ,title)
62
          (meta (@ (name "viewport") (content "width=device-width, initial-scale=1"))))
63
         (body ,@body)))
64
65
(define (post-handler request request-body pb-data-path)
66
  (if (eq? (request-method request) 'POST)
67
      (let* ((headers (request-headers request))
68
             (content-type-all (assq-ref headers 'content-type))
69
             (content-type (if content-type-all
70
                               (car content-type-all)
71
                               #f))
72
             (boundary (if (eq? content-type 'multipart/form-data)
73
                           (assq-ref (cdr content-type-all) 'boundary)
74
                           #f))
75
             (reqbody-string (utf8->string request-body))
76
             (form-data (if boundary
77
                            (read-multipart-form-data reqbody-string boundary)
78
                            #f))
79
             (new-pb-data (if form-data
80
                              (call-with-dir-as-pb-data
81
                               pb-data-path
82
                               (lambda (pb-data)
83
                                 (pb-data-new-entry pb-data
84
                                                    (assoc-ref form-data "text"))))
85
                              #f)))
86
87
        ;; determine what to respond
88
        (if (and new-pb-data
89
                 (assoc-ref form-data "showUrl"))
90
91
            ;; show url after paste
92
            (values (build-response
93
                     #:code 200
94
                     #:headers '((content-type . (text/plain))))
95
                    (lambda (port)
96
                      (let* ((hostp (assq-ref headers 'host)))
97
                        (put-string
98
                         port
99
                         (uri->string
100
                          (build-uri 'http
101
                                     #:host (car hostp)
102
                                     #:port (cdr hostp)
103
                                     #:path (format #f "/raw/~a\r\n"
104
                                                    (pb-entry-id new-pb-data))))))))
105
106
            ;; respond with 303 See Other
107
            (values (build-response
108
                     #:code 303
109
                     #:headers `((location . ,(build-uri-reference #:path "/"))))
110
                    (lambda (port) 1))))
111
112
      ;; INVALID request: access /post without HTTP POST
113
      (values (build-response #:code 400)
114
              (lambda (port) 1))))
115
116
(define (raw-handler pb-data-path pb-id)
117
  (if (pb-entry-id-valid? pb-id)
118
      (values (build-response
119
               #:code 200
120
               #:headers '((content-type . (text/plain))))
121
              (lambda (port)
122
                (call-with-input-file
123
                    ;; the file name
124
                    (call-with-dir-as-pb-data
125
                     pb-data-path
126
                     (lambda (p) (pb-get-file-path p pb-id)))
127
                  ;; the input port
128
                  (lambda (inport)
129
                    (let A ((inport' inport))
130
                      (let ((bv (get-bytevector-n inport' 4096)))
131
                        (if (not (eof-object? bv))
132
                            (begin
133
                              (put-bytevector port bv)
134
                              (A inport')))))))))
135
      (values (build-response #:code 404)
136
              (lambda (port) 1))))
137
138
(define (make-pastebin-handler data-path)
139
  (lambda (request request-body)
140
    (match (split-and-decode-uri-path (uri-path (request-uri request)))
141
142
      ;; URI: /post -- create paste
143
      (("post" . _)
144
       (post-handler request request-body data-path))
145
146
      ;; URI: /raw/<id> -- return raw content of the paste
147
      (("raw" pb-id)
148
       (raw-handler data-path pb-id))
149
150
      ;; URI: * -- everything else -- show the top 5 paste list
151
      (_
152
       (values (build-response
153
                #:code 200
154
                #:headers '((content-type . (text/html))))
155
156
               (lambda (port)
157
                 (let* ((top5 (call-with-dir-as-pb-data
158
                               data-path
159
                               (lambda (pb-data) (pb-data-get-top pb-data 5))))
160
                        (sxml (templatize
161
                               "pastebin"
162
                               `((form (@ (method "post") (enctype "multipart/form-data")
163
                                          (action "/post"))
164
                                       (textarea (@ (name "text")) "")
165
                                       (input (@ (type "checkbox") (name "showUrl")
166
                                                 (id "showUrl") (value "1")))
167
                                       (label (@ (for "showUrl")) "Show raw URL after paste")
168
                                       (input (@ (type "submit"))))
169
                                 (table (@ (border 1)) (tr (th "id") (th "text") (th ""))
170
                                        ,(map (lambda (entry)
171
                                                `(tr (td ,(pb-entry-id entry))
172
                                                     (td ,(pb-entry-text entry))
173
                                                     (td
174
                                                      (a (@ (href
175
                                                             ,(format #f "/raw/~a"
176
                                                                      (pb-entry-id entry))))
177
                                                         "raw"))))
178
                                              top5))))))
179
                   (display "<!DOCTYPE html>\n" port)
180
                   (sxml->xml sxml port))))))))
181