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 (@ (lang "en"))
61
         (head
62
          (title ,title)
63
          (meta (@ (charset "utf-8")))
64
          (meta (@ (name "viewport") (content "width=device-width, initial-scale=1.0")))
65
          (link (@ (rel "stylesheet") (href "https://unpkg.com/simpledotcss/simple.min.css"))))
66
         (body ,@body)))
67
68
(define (post-handler request request-body pb-data-path)
69
  (if (eq? (request-method request) 'POST)
70
      (let* ((headers (request-headers request))
71
             (content-type-all (assq-ref headers 'content-type))
72
             (content-type (if content-type-all
73
                               (car content-type-all)
74
                               #f))
75
             (boundary (if (eq? content-type 'multipart/form-data)
76
                           (assq-ref (cdr content-type-all) 'boundary)
77
                           #f))
78
             (reqbody-string (utf8->string request-body))
79
             (form-data (if boundary
80
                            (read-multipart-form-data reqbody-string boundary)
81
                            #f))
82
             (new-pb-data (if form-data
83
                              (call-with-dir-as-pb-data
84
                               pb-data-path
85
                               (lambda (pb-data)
86
                                 (pb-data-new-entry pb-data
87
                                                    (assoc-ref form-data "text"))))
88
                              #f)))
89
90
        ;; determine what to respond
91
        (if (and new-pb-data
92
                 (assoc-ref form-data "showUrl"))
93
94
            ;; show url after paste
95
            (values (build-response
96
                     #:code 200
97
                     #:headers '((content-type . (text/plain))))
98
                    (lambda (port)
99
                      (let* ((hostp (assq-ref headers 'host)))
100
                        (put-string
101
                         port
102
                         (uri->string
103
                          (build-uri 'http
104
                                     #:host (car hostp)
105
                                     #:port (cdr hostp)
106
                                     #:path (format #f "/raw/~a\r\n"
107
                                                    (pb-entry-id new-pb-data))))))))
108
109
            ;; respond with 303 See Other
110
            (values (build-response
111
                     #:code 303
112
                     #:headers `((location . ,(build-uri-reference #:path "/"))))
113
                    (lambda (port) 1))))
114
115
      ;; INVALID request: access /post without HTTP POST
116
      (values (build-response #:code 400)
117
              (lambda (port) 1))))
118
119
(define (raw-handler pb-data-path pb-id)
120
  (if (pb-entry-id-valid? pb-id)
121
      (values (build-response
122
               #:code 200
123
               #:headers '((content-type . (text/plain))))
124
              (lambda (port)
125
                (call-with-input-file
126
                    ;; the file name
127
                    (call-with-dir-as-pb-data
128
                     pb-data-path
129
                     (lambda (p) (pb-get-file-path p pb-id)))
130
                  ;; the input port
131
                  (lambda (inport)
132
                    (let A ((inport' inport))
133
                      (let ((bv (get-bytevector-n inport' 4096)))
134
                        (if (not (eof-object? bv))
135
                            (begin
136
                              (put-bytevector port bv)
137
                              (A inport')))))))))
138
      (values (build-response #:code 404)
139
              (lambda (port) 1))))
140
141
(define (make-pastebin-handler data-path)
142
  (lambda (request request-body)
143
    (match (split-and-decode-uri-path (uri-path (request-uri request)))
144
145
      ;; URI: /post -- create paste
146
      (("post" . _)
147
       (post-handler request request-body data-path))
148
149
      ;; URI: /raw/<id> -- return raw content of the paste
150
      (("raw" pb-id)
151
       (raw-handler data-path pb-id))
152
153
      ;; URI: * -- everything else -- show the top 5 paste list
154
      (_
155
       (values (build-response
156
                #:code 200
157
                #:headers '((content-type . (text/html))))
158
159
               (lambda (port)
160
                 (let* ((top5 (call-with-dir-as-pb-data
161
                               data-path
162
                               (lambda (pb-data) (pb-data-get-top pb-data 5))))
163
                        (sxml (templatize
164
                               "Pastebin"
165
                               `((header (h1 "Pastebin"))
166
                                 (main (section (h2 "Recent")
167
                                                ,(map (lambda (entry)
168
                                                        `(article (h3 (a (@ (href ,(format #f "/raw/~a" (pb-entry-id entry)))
169
                                                                            (target "_blank"))
170
                                                                         ,(pb-entry-id entry)))
171
                                                                  (pre ,(pb-entry-text entry))))
172
                                                      top5))
173
                                       (section (h2 "New")
174
                                                (form (@ (method "post")
175
                                                         (enctype "multipart/form-data")
176
                                                         (action "/post"))
177
                                                      (p (label "Paste content"
178
                                                                (textarea (@ (name "text")) "")))
179
                                                      (p (label (input (@ (type "checkbox")
180
                                                                          (name "showUrl")
181
                                                                          (value "1")))
182
                                                                "Show raw URL after paste"))
183
                                                      (input (@ (type "submit"))))))
184
                                 (footer (p (a (@ (href "https://github.com/pisemsky/guile-pastebin")
185
                                                  (target "_blank"))
186
                                               "Source code")))))))
187
                   (display "<!DOCTYPE html>\n" port)
188
                   (sxml->xml sxml port))))))))
189