in
Clone
https://git.tahinli.com/Tahinli/in.git
0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits
| 1 | //! The one function that sends a stored blob to a browser. Every byte route (download, raw, and the |
| 2 | //! shared and public ones that come later) calls [`serve_bytes`], so the headers and the range rules |
| 3 | //! exist in exactly one place (architecture section 7). |
| 4 | //! |
| 5 | //! The caller has already decided that the viewer may see the bytes (`authz`). This module decides how |
| 6 | //! they are sent: a single byte range, a validator (`ETag`) and its conditional requests, a disposition |
| 7 | //! that is `inline` only for types a browser can show without running them, and the response headers |
| 8 | //! that stop the bytes from being used as a page. The body is streamed from the blob store in frames of |
| 9 | //! [`in_core::blob::FRAME_BYTES`]; nothing is buffered whole. |
| 10 | //! |
| 11 | //! Integrity rules: a full-body answer is hashed while it streams and aborted when the hash does not match |
| 12 | //! the row (a changed blob, which the `ETag` would otherwise hide). A range answer is not checked: it is a |
| 13 | //! part of the file, and a partial read cannot be compared with the whole-file hash. `in fsck --hash` |
| 14 | //! checks every blob in full. |
| 15 | //! |
| 16 | //! Range rules (RFC 7233 section 3.1): one `bytes=` range is honoured (`a-b`, `a-`, `-n`). A request with |
| 17 | //! several ranges gets the whole file with `200`. A header that is syntactically invalid, reversed, or in |
| 18 | //! an unknown unit is ignored and gets the whole file with `200`. A valid range the file cannot satisfy |
| 19 | //! gets `416` with `Content-Range: bytes */<len>`. `If-Range` with a validator that is not the current one |
| 20 | //! gets the whole file. |
| 21 | |
| 22 | use TryStreamExt; |
| 23 | use |
| 24 | |
| 25 | |
| 26 | |
| 27 | ; |
| 28 | use Frame; |
| 29 | use StreamBody; |
| 30 | use ; |
| 31 | use Result; |
| 32 | use Cx; |
| 33 | use headers; |
| 34 | use ; |
| 35 | |
| 36 | /// What a browser may do with a byte response: show it in the page, or save it. |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | /// Everything about a stored file that a byte response needs. The caller fills it from the row. |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | /// The status, headers and body of a byte response. Handlers return it as they are. |
| 67 | pub type ByteAnswer = ; |
| 68 | |
| 69 | /// `Content-Security-Policy` of every byte response. The sandbox makes the document opaque, and |
| 70 | /// `default-src 'none'` stops it from loading anything but its own image, media or style. |
| 71 | /// Only the viewer page of this origin may frame the bytes. |
| 72 | pub const BYTES_CSP: &str = "sandbox; default-src 'none'; img-src 'self'; media-src 'self'; \ |
| 73 | style-src 'unsafe-inline'; frame-ancestors 'self'"; |
| 74 | |
| 75 | /// The same policy without `sandbox`, for PDFs only: browsers refuse to run their PDF viewer in a |
| 76 | /// sandboxed document. A PDF is never interpreted as HTML (`nosniff`, fixed type), and the viewers |
| 77 | /// keep PDF scripts away from the origin. |
| 78 | pub const PDF_CSP: &str = "default-src 'none'; img-src 'self'; media-src 'self'; \ |
| 79 | style-src 'unsafe-inline'; frame-ancestors 'self'"; |
| 80 | |
| 81 | /// Cache policy of the member's own bytes (`/file` and `/thumb`). Access is revoked by a grant change, not by |
| 82 | /// the URL, so a cached copy must not outlive the check: the browser revalidates, and the `ETag` turns that |
| 83 | /// into a cheap `304` while the bytes are unchanged. |
| 84 | const BYTES_CACHE: &str = "private, no-cache"; |
| 85 | |
| 86 | /// Cache policy of bytes behind a public share link. The link can be revoked or its password changed, so |
| 87 | /// a cached copy must not outlive the check: the browser asks again every time. |
| 88 | pub const SHARE_CACHE: &str = "private, no-cache"; |
| 89 | |
| 90 | /// The outcome of reading a `Range` header against a length. |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | /// Parses the value of a `Range` header for a file of `len` bytes. |
| 107 | /// |
| 108 | /// Accepted: `bytes=a-b` (clamped to the end of the file), `bytes=a-` (to the end), `bytes=-n` (the last |
| 109 | /// `n` bytes; a longer `n` means the whole file). Several ranges, separated by commas, give |
| 110 | /// [`Ranged::Full`]. |
| 111 | /// |
| 112 | /// A header that is not valid syntax is ignored, so it gives [`Ranged::Full`] too: a unit other than |
| 113 | /// `bytes`, a missing `-`, a non-digit, or a last byte before the first. A valid range the file cannot |
| 114 | /// satisfy is [`Ranged::Unsatisfiable`]: a first byte at or past the end, a suffix of `-0`, or any range |
| 115 | /// on an empty file. A number too large for `u64` is valid digits, so it saturates: as a first byte it is |
| 116 | /// past the end, as a last byte it is clamped. |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | /// A non-empty run of ASCII digits as a number. Anything else is `None`; a value too large for `u64` |
| 161 | /// saturates to `u64::MAX`. |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | /// The validator of a stored file's bytes. It is keyed (see [`in_core::secret::validator`]), so a viewer of |
| 170 | /// the bytes learns nothing about the file's SHA-256 from the `ETag`, and cannot tell whether a file they know |
| 171 | /// is stored in the drive. |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | /// Whether a conditional request `If-None-Match` names `etag`. Weak tags compare equal to their strong |
| 177 | /// form here, which is the rule for `GET` validators (RFC 9110). |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | /// Whether a range may be honoured under `If-Range`. Without the header it always may. With it, only a |
| 190 | /// strong validator equal to `etag` allows the range; a date or a weak tag never does. |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | /// The range that applies to this request, after `If-Range` has had its say. |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | /// Whether serving this request counts as a download that started at byte 0. A `304`, a range that |
| 210 | /// starts later, and an unsatisfiable range do not count; a whole file, or a range from byte 0, does. |
| 211 | /// The download counter is bumped by the caller before it serves the body. `validator` is the bytes' |
| 212 | /// validator, as sent in [`ByteSource::validator`]. |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | /// Whether a type may be shown inline. SVG is never inline: it can carry script. HTML is never inline. |
| 226 | /// Images (except SVG and HEIC/HEIF, which browsers do not show everywhere), audio, video, PDF and plain |
| 227 | /// text are. |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | /// The `Content-Type` value for a stored type. Plain text is always sent as UTF-8. |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | /// Characters that may appear unescaped in an RFC 5987 `filename*` value (`attr-char`). |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | /// The `Content-Disposition` header for a file name: `inline` or `attachment`, a quoted ASCII fallback |
| 261 | /// and the UTF-8 name percent-encoded per RFC 5987. |
| 262 | /// |
| 263 | /// The fallback keeps printable ASCII except the quote and the backslash, and turns everything else |
| 264 | /// (control characters, including CR and LF, non-ASCII letters) into `_`. So no name can close the |
| 265 | /// quoted string or start a new header line. The `filename*` value encodes every byte that is not an |
| 266 | /// `attr-char`, which covers the same characters. |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | /// The headers every byte response carries, whatever its status. |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | /// Inserts a header whose value is text the code controls. A value that is not a valid header value is |
| 320 | /// dropped, never sent half-formed; every caller passes ASCII that the code made. |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | /// The `416` answer: no body, and the length the client may ask again within. |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | /// Sends the bytes of `source` for the request in `cx`. |
| 335 | /// |
| 336 | /// `disposition` is the caller's wish. It becomes [`Disposition::Attachment`] unless the type is in |
| 337 | /// [`inline_safe`]. The body streams from the blob store from the first byte the answer needs. A |
| 338 | /// missing blob is an error (the row names bytes that are not there), which the router answers as a |
| 339 | /// `500`. |
| 340 | pub async |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | /// [`serve_bytes`] with the cache policy given: the owner's bytes use [`BYTES_CACHE`], share-link bytes |
| 350 | /// use [`SHARE_CACHE`]. Everything else is the same. |
| 351 | pub async |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | /// Turns a blob stream into a response body. Frames are passed through as they come. |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 |