omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / apps / interplane-forge / tools / bring / tests / standin.om
| 1 | om2 |
| 2 | note tools/bring/tests/standin.om: the stand-in Interplane hub for the tool's tests (Linux, pinned omc). |
| 3 | note standin serve PORT DIR answers the contract's sign-in, /open/, /open/up chunk + finish, status, |
| 4 | note shelf JSON and archive on 127.0.0.1:PORT, one connection at a time |
| 5 | note standin count PORT DIR accepts and closes connections, appending one line per connection |
| 6 | note Every request line is appended to DIR/log; every accepted connection to DIR/conns. Saved: |
| 7 | note DIR/login.form, DIR/up.b64 (chunk data in seq order), DIR/finish.form. GET /login starts a new |
| 8 | note upload (the injection and status state are reset). Injections (when the |
| 9 | note file DIR/inject exists): the first try of seq 1 answers 429, and the first seq 2 answers |
| 10 | note "409 next 1" (the client must resend chunk 1). The status JSON says "queued" once, then "stored" |
| 11 | note (or the line in DIR/refuse as "refused: <line>" when that file exists). GET /open/<rid>.json |
| 12 | note serves DIR/shelf.json and GET /open/<rid>.tar serves DIR/shelf.tar. Busy injections: DIR/busyj |
| 13 | note (for the JSON) and DIR/busyt (for the archive) hold 3-digit codes, e.g. "503503"; each GET |
| 14 | note answers the first code with an empty body and drops it; a "+" after the first code ("503+") |
| 15 | note answers it every time. An empty or absent file serves normally. DIR/busyp does the same for chunk |
| 16 | note POSTs and DIR/busyf for the finish POST (nothing is saved for a busy answer). |
| 17 | note Withdraw: POST /open/ (origin, session, csrf as for /open/up; DIR/busyp applies) saves its form to |
| 18 | note DIR/withdraw.form, answers 303 with Location: /open/ and writes DIR/gone = "1": the next shelf |
| 19 | note JSON GET still serves DIR/shelf.json (the intake has not run yet) and sets it to "2"; from then on |
| 20 | note the JSON is 404. When DIR/signout exists it answers 303 with Location: /login and saves nothing |
| 21 | note (the hub when the session is gone). |
| 22 | note Patch: POST /alice/demo/patches/new (session and csrf as for /open/up) is answered from DIR/busyq, |
| 23 | note 3-digit codes as in DIR/busyp: a 429 with the forge's write-gate reason ("refused: too many |
| 24 | note writes ..."), any other code with an empty body; no code saves the form to DIR/patch.form and |
| 25 | note answers 201 with "path /alice/demo/patches/1". Every POST is one line of DIR/log. |
| 26 | text tserve "serve" |
| 27 | text tcount "count" |
| 28 | text tlog "log" |
| 29 | text tconns "conns" |
| 30 | text tconn "conn\n" |
| 31 | text tlogin "login.form" |
| 32 | text tupb "up.b64" |
| 33 | text tfin "finish.form" |
| 34 | text tinject "inject" |
| 35 | text trefuse "refuse" |
| 36 | text tshelfj "shelf.json" |
| 37 | text tshelft "shelf.tar" |
| 38 | text tbusyj "busyj" |
| 39 | text tbusyt "busyt" |
| 40 | text tbusyp "busyp" |
| 41 | text tbusyf "busyf" |
| 42 | text tbusyq "busyq" |
| 43 | text tptform "patch.form" |
| 44 | text tpostpt "POST /alice/demo/patches/new " |
| 45 | text tgetset "GET /settings " |
| 46 | text tptpath "path /alice/demo/patches/1\n" |
| 47 | text tptgate "refused: too many writes from this address or key; wait a minute\n" |
| 48 | text tgone "gone" |
| 49 | text twdform "withdraw.form" |
| 50 | text tpostop "POST /open/ " |
| 51 | text tone "1" |
| 52 | text tsignout "signout" |
| 53 | text tlocop "Location: /open/\n" |
| 54 | text tloclg "Location: /login\n" |
| 55 | text tgetlog "GET /login " |
| 56 | text tpostlg "POST /login " |
| 57 | text tgetop "GET /open/ " |
| 58 | text tpostup "POST /open/up " |
| 59 | text tgetup "GET /open/up/" |
| 60 | text tgetopn "GET /open/" |
| 61 | text tjs ".json " |
| 62 | text ttar ".tar " |
| 63 | text tpreck "__Host-forge_pre=0123456789abcdef0123456789abcdef" |
| 64 | text tsessck "__Host-forge_session=feedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedface" |
| 65 | text tcsrf1 "csrf=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 66 | text tcsrf2 "csrf=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" |
| 67 | text torig "\norigin: http://127.0.0.1:" |
| 68 | text tlpage "<form><input type=\"hidden\" name=\"csrf\" value=\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\">\n<input type=\"hidden\" name=\"nonce\" value=\"00112233445566778899aabbccddeeff\">\n<input type=\"hidden\" name=\"ts\" value=\"1762848000\"></form>\n" |
| 69 | text topage "<form><input type=\"hidden\" name=\"csrf\" value=\"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"></form>\n" |
| 70 | text tsc1 "Set-Cookie: __Host-forge_pre=0123456789abcdef0123456789abcdef; Max-Age=900; HttpOnly; Secure; SameSite=Lax; Path=/\n" |
| 71 | text tsc2 "Set-Cookie: __Host-forge_session=feedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedfacefeedface; Max-Age=2592000; HttpOnly; Secure; SameSite=Lax; Path=/\nLocation: /\n" |
| 72 | text tachunk "action=chunk" |
| 73 | text tafin "action=finish" |
| 74 | text tseq "&seq=" |
| 75 | text tdata "&data=" |
| 76 | text tsha "&sha=" |
| 77 | text tnext "next " |
| 78 | text tqueued "queued " |
| 79 | text tj1 "{\"schema\":\"interplane/open-upload/1\",\"state\":\"queued\",\"rid\":null,\"page\":null}\n" |
| 80 | text tj2 "{\"schema\":\"interplane/open-upload/1\",\"state\":\"stored\",\"rid\":\"rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5\",\"page\":\"/open/z3gqcJUoA1n9HaHKufZs5FCSGazv5\"}\n" |
| 81 | text tj3a "{\"schema\":\"interplane/open-upload/1\",\"state\":\"refused: " |
| 82 | text tj3b "\",\"rid\":null,\"page\":null}\n" |
| 83 | text th1 "HTTP/1.1 " |
| 84 | text th2 "\nContent-Type: " |
| 85 | text th3 "\nContent-Length: " |
| 86 | text th4 "\nConnection: close\n" |
| 87 | text tplain "text/plain" |
| 88 | text thtml "text/html" |
| 89 | text tjson "application/json" |
| 90 | text tbin "application/x-tar" |
| 91 | text tclen "content-length:" |
| 92 | text tcookie "cookie:" |
| 93 | text tusage "usage: standin serve|count PORT DIR\n" |
| 94 | global gst ulong 8 |
| 95 | |
| 96 | fn wall fd int b view uchar * to bool |
| 97 | n = ulong 0 |
| 98 | while lt n len b bound 9223372036854775807 |
| 99 | r = write fd b n sub len b n |
| 100 | if le r 0 |
| 101 | ret false |
| 102 | end |
| 103 | n = add n ulong r |
| 104 | end |
| 105 | ret true |
| 106 | end |
| 107 | note hput: text with each LF written as CR LF |
| 108 | fn hput fd int t view uchar * to void |
| 109 | cr = new uchar 2 0 |
| 110 | put cr 0 13 |
| 111 | put cr 1 10 |
| 112 | s = ulong 0 |
| 113 | for i ulong len t bound 9223372036854775807 |
| 114 | if eq get t i 10 |
| 115 | x = wall fd part t s sub i s |
| 116 | x = wall fd cr |
| 117 | s = add i 1 |
| 118 | end |
| 119 | end |
| 120 | x = wall fd part t s sub len t s |
| 121 | end |
| 122 | fn decw v ulong o edit uchar * n ulong to ulong |
| 123 | d = new uchar 20 0 |
| 124 | k = ulong 0 |
| 125 | x = v |
| 126 | while true bound 21 |
| 127 | put d k uchar add 48 rem x 10 |
| 128 | k = add k 1 |
| 129 | x = div x 10 |
| 130 | if eq x 0 |
| 131 | break |
| 132 | end |
| 133 | end |
| 134 | for i ulong k |
| 135 | put o add n i get d sub sub k i 1 |
| 136 | end |
| 137 | ret add n k |
| 138 | end |
| 139 | fn bat b view uchar * i ulong t view uchar * to bool |
| 140 | if gt add i len t len b |
| 141 | ret false |
| 142 | end |
| 143 | for k ulong len t bound 9223372036854775807 |
| 144 | if ne get b add i k get t k |
| 145 | ret false |
| 146 | end |
| 147 | end |
| 148 | ret true |
| 149 | end |
| 150 | fn lcb c uchar to uchar |
| 151 | if and ge c 65 le c 90 |
| 152 | ret add c 32 |
| 153 | end |
| 154 | ret c |
| 155 | end |
| 156 | fn bati b view uchar * i ulong t view uchar * to bool |
| 157 | if gt add i len t len b |
| 158 | ret false |
| 159 | end |
| 160 | for k ulong len t bound 9223372036854775807 |
| 161 | if ne lcb get b add i k get t k |
| 162 | ret false |
| 163 | end |
| 164 | end |
| 165 | ret true |
| 166 | end |
| 167 | fn bfind b view uchar * t view uchar * to long |
| 168 | i = ulong 0 |
| 169 | while le add i len t len b bound 9223372036854775807 |
| 170 | if bat b i t |
| 171 | ret long i |
| 172 | end |
| 173 | i = add i 1 |
| 174 | end |
| 175 | ret -1 |
| 176 | end |
| 177 | fn bfindi b view uchar * t view uchar * to long |
| 178 | i = ulong 0 |
| 179 | while le add i len t len b bound 9223372036854775807 |
| 180 | if bati b i t |
| 181 | ret long i |
| 182 | end |
| 183 | i = add i 1 |
| 184 | end |
| 185 | ret -1 |
| 186 | end |
| 187 | note decat: the decimal number at b[i..] (stops at the first non-digit) |
| 188 | fn decat b view uchar * i ulong to ulong |
| 189 | v = ulong 0 |
| 190 | j = i |
| 191 | while lt j len b bound 9223372036854775807 |
| 192 | c = get b j |
| 193 | if or lt c 48 gt c 57 |
| 194 | break |
| 195 | end |
| 196 | v = add mul v 10 ulong sub c 48 |
| 197 | j = add j 1 |
| 198 | end |
| 199 | ret v |
| 200 | end |
| 201 | fn appendf nm view uchar * b view uchar * to void |
| 202 | fd = fopen nm 0 len nm 525377 384 |
| 203 | if lt fd 0 |
| 204 | exit 3 |
| 205 | end |
| 206 | x = wall int fd b |
| 207 | y = fclose int fd |
| 208 | end |
| 209 | fn exists nm view uchar * to bool |
| 210 | fd = fopen nm 0 len nm 524288 0 |
| 211 | if lt fd 0 |
| 212 | ret false |
| 213 | end |
| 214 | x = fclose int fd |
| 215 | ret true |
| 216 | end |
| 217 | note answer: status line, headers (extra holds whole header lines), body |
| 218 | fn hct fd int ct ulong to void |
| 219 | if eq ct 0 |
| 220 | hput fd tplain |
| 221 | end |
| 222 | if eq ct 1 |
| 223 | hput fd thtml |
| 224 | end |
| 225 | if eq ct 2 |
| 226 | hput fd tjson |
| 227 | end |
| 228 | if eq ct 3 |
| 229 | hput fd tbin |
| 230 | end |
| 231 | end |
| 232 | fn answer fd int code ulong ct ulong ex ulong body view uchar * to void |
| 233 | ahead fd code ct ex len body |
| 234 | x = wall fd body |
| 235 | end |
| 236 | fn ahead fd int code ulong ct ulong ex ulong blen ulong to void |
| 237 | d = new uchar 24 0 |
| 238 | k = decw code d 0 |
| 239 | hput fd th1 |
| 240 | x = wall fd part d 0 k |
| 241 | hput fd th2 |
| 242 | hct fd ct |
| 243 | hput fd th3 |
| 244 | k = decw blen d 0 |
| 245 | x = wall fd part d 0 k |
| 246 | hput fd th4 |
| 247 | if eq ex 1 |
| 248 | hput fd tsc1 |
| 249 | end |
| 250 | if eq ex 2 |
| 251 | hput fd tsc2 |
| 252 | end |
| 253 | if eq ex 3 |
| 254 | hput fd tlocop |
| 255 | end |
| 256 | if eq ex 4 |
| 257 | hput fd tloclg |
| 258 | end |
| 259 | cr = new uchar 2 0 |
| 260 | put cr 0 13 |
| 261 | put cr 1 10 |
| 262 | x = wall fd cr |
| 263 | end |
| 264 | fn answerf fd int ct ulong nm view uchar * to void |
| 265 | f = fopen nm 0 len nm 524288 0 |
| 266 | if lt f 0 |
| 267 | answer fd 404 0 0 part tlog 0 0 |
| 268 | ret |
| 269 | end |
| 270 | st = new uchar 128 0 |
| 271 | r = fstat int f st 0 128 |
| 272 | sz = ulong 0 |
| 273 | for i ulong 8 |
| 274 | sz = or sz shl ulong get st add 48 i mul i 8 |
| 275 | end |
| 276 | ahead fd 200 ct 0 sz |
| 277 | b = new uchar 65536 0 |
| 278 | while true bound 9223372036854775807 |
| 279 | n = read int f b 0 len b |
| 280 | if le n 0 |
| 281 | break |
| 282 | end |
| 283 | y = wall fd part b 0 ulong n |
| 284 | end |
| 285 | x = fclose int f |
| 286 | end |
| 287 | note readreq: one request (headers and Content-Length body) into rq; returns its length or 0 |
| 288 | fn readreq rq edit uchar * fd int to ulong |
| 289 | n = ulong 0 |
| 290 | hend = long -1 |
| 291 | while true bound 9223372036854775807 |
| 292 | if ge n len rq |
| 293 | ret 0 |
| 294 | end |
| 295 | r = read fd rq n sub len rq n |
| 296 | if le r 0 |
| 297 | ret 0 |
| 298 | end |
| 299 | n = add n ulong r |
| 300 | hend = hdrend rq n |
| 301 | if ge hend 0 |
| 302 | break |
| 303 | end |
| 304 | end |
| 305 | want = add ulong hend bodylen rq ulong hend |
| 306 | if gt want len rq |
| 307 | ret 0 |
| 308 | end |
| 309 | while lt n want bound 9223372036854775807 |
| 310 | r = read fd rq n sub want n |
| 311 | if le r 0 |
| 312 | ret 0 |
| 313 | end |
| 314 | n = add n ulong r |
| 315 | end |
| 316 | ret n |
| 317 | end |
| 318 | fn hdrend rq edit uchar * n ulong to long |
| 319 | for i ulong n bound 9223372036854775807 |
| 320 | if gt add i 4 n |
| 321 | ret -1 |
| 322 | end |
| 323 | if and eq get rq i 13 eq get rq add i 1 10 |
| 324 | if and eq get rq add i 2 13 eq get rq add i 3 10 |
| 325 | ret long add i 4 |
| 326 | end |
| 327 | end |
| 328 | end |
| 329 | ret -1 |
| 330 | end |
| 331 | fn bodylen rq edit uchar * h ulong to ulong |
| 332 | p = bfindi part rq 0 h tclen |
| 333 | if lt p 0 |
| 334 | ret 0 |
| 335 | end |
| 336 | i = add ulong p len tclen |
| 337 | while eq get rq i 32 bound 9223372036854775807 |
| 338 | i = add i 1 |
| 339 | end |
| 340 | ret decat part rq 0 h i |
| 341 | end |
| 342 | fn logline rq edit uchar * n ulong to void |
| 343 | e = ulong 0 |
| 344 | while lt e n bound 9223372036854775807 |
| 345 | if eq get rq e 13 |
| 346 | break |
| 347 | end |
| 348 | e = add e 1 |
| 349 | end |
| 350 | b = new uchar 8192 0 |
| 351 | if gt e 8000 |
| 352 | e = ulong 8000 |
| 353 | end |
| 354 | for i ulong e bound 8000 |
| 355 | put b i get rq i |
| 356 | end |
| 357 | put b e 10 |
| 358 | appendf tlog part b 0 add e 1 |
| 359 | end |
| 360 | note has: the request (headers or body) holds t |
| 361 | fn has rq edit uchar * n ulong t view uchar * to bool |
| 362 | ret ge bfind part rq 0 n t 0 |
| 363 | end |
| 364 | fn hasi rq edit uchar * n ulong t view uchar * to bool |
| 365 | ret ge bfindi part rq 0 n t 0 |
| 366 | end |
| 367 | fn route rq edit uchar * fd int n ulong port ulong to void |
| 368 | if bat part rq 0 n 0 tgetlog |
| 369 | for i ulong 8 |
| 370 | put gst i 0 |
| 371 | end |
| 372 | answer fd 200 1 1 tlpage |
| 373 | ret |
| 374 | end |
| 375 | if bat part rq 0 n 0 tgetopn |
| 376 | if not bat part rq 0 n 0 tgetop |
| 377 | if not bat part rq 0 n 0 tgetup |
| 378 | public rq fd n |
| 379 | ret |
| 380 | end |
| 381 | end |
| 382 | end |
| 383 | if not origin rq n port |
| 384 | if bat part rq 0 n 0 tpostlg |
| 385 | answer fd 403 0 0 part tlog 0 0 |
| 386 | ret |
| 387 | end |
| 388 | if bat part rq 0 n 0 tpostup |
| 389 | answer fd 403 0 0 part tlog 0 0 |
| 390 | ret |
| 391 | end |
| 392 | if bat part rq 0 n 0 tpostop |
| 393 | answer fd 403 0 0 part tlog 0 0 |
| 394 | ret |
| 395 | end |
| 396 | end |
| 397 | if bat part rq 0 n 0 tpostlg |
| 398 | if or not has rq n tpreck not has rq n tcsrf1 |
| 399 | answer fd 403 0 0 part tlog 0 0 |
| 400 | ret |
| 401 | end |
| 402 | savebody rq n tlogin |
| 403 | answer fd 303 0 2 part tlog 0 0 |
| 404 | ret |
| 405 | end |
| 406 | if not has rq n tsessck |
| 407 | answer fd 403 0 0 part tlog 0 0 |
| 408 | ret |
| 409 | end |
| 410 | if bat part rq 0 n 0 tgetop |
| 411 | answer fd 200 1 0 topage |
| 412 | ret |
| 413 | end |
| 414 | if bat part rq 0 n 0 tpostup |
| 415 | if not has rq n tcsrf2 |
| 416 | answer fd 403 0 0 part tlog 0 0 |
| 417 | ret |
| 418 | end |
| 419 | postup rq fd n |
| 420 | ret |
| 421 | end |
| 422 | if bat part rq 0 n 0 tpostop |
| 423 | if not has rq n tcsrf2 |
| 424 | answer fd 403 0 0 part tlog 0 0 |
| 425 | ret |
| 426 | end |
| 427 | if busyans fd tbusyp |
| 428 | ret |
| 429 | end |
| 430 | if exists tsignout |
| 431 | answer fd 303 0 4 part tlog 0 0 |
| 432 | ret |
| 433 | end |
| 434 | savebody rq n twdform |
| 435 | g = fopen tgone 0 len tgone 524865 384 |
| 436 | x = wall int g tone |
| 437 | y = fclose int g |
| 438 | answer fd 303 0 3 part tlog 0 0 |
| 439 | ret |
| 440 | end |
| 441 | if bat part rq 0 n 0 tgetup |
| 442 | status fd |
| 443 | ret |
| 444 | end |
| 445 | if bat part rq 0 n 0 tgetset |
| 446 | answer fd 200 1 0 topage |
| 447 | ret |
| 448 | end |
| 449 | if bat part rq 0 n 0 tpostpt |
| 450 | if not has rq n tcsrf2 |
| 451 | answer fd 403 0 0 part tlog 0 0 |
| 452 | ret |
| 453 | end |
| 454 | postpt rq fd n |
| 455 | ret |
| 456 | end |
| 457 | answer fd 404 0 0 part tlog 0 0 |
| 458 | end |
| 459 | note public: the shelf JSON and the archive need no session (as forged serves them) |
| 460 | fn public rq edit uchar * fd int n ulong to void |
| 461 | if has rq n tjs |
| 462 | if busyans fd tbusyj |
| 463 | ret |
| 464 | end |
| 465 | if gonejs fd |
| 466 | ret |
| 467 | end |
| 468 | answerf fd 2 tshelfj |
| 469 | ret |
| 470 | end |
| 471 | if has rq n ttar |
| 472 | if busyans fd tbusyt |
| 473 | ret |
| 474 | end |
| 475 | answerf fd 3 tshelft |
| 476 | ret |
| 477 | end |
| 478 | answer fd 404 0 0 part tlog 0 0 |
| 479 | end |
| 480 | note gonejs: after a withdraw, "1" in DIR/gone serves the shelf once more and becomes "2"; "2" is 404 |
| 481 | fn gonejs fd int to bool |
| 482 | f = fopen tgone 0 len tgone 524290 0 |
| 483 | if lt f 0 |
| 484 | ret false |
| 485 | end |
| 486 | b = new uchar 1 0 |
| 487 | r = read int f b 0 1 |
| 488 | if and eq r 1 eq get b 0 49 |
| 489 | put b 0 50 |
| 490 | w = pwrite int f b 0 1 0 |
| 491 | x = fclose int f |
| 492 | ret false |
| 493 | end |
| 494 | x = fclose int f |
| 495 | answer fd 404 0 0 part tlog 0 0 |
| 496 | ret true |
| 497 | end |
| 498 | note busyans: the first 3-digit code in DIR/nm, answered with an empty body; true when one was sent |
| 499 | fn busyans fd int nm view uchar * to bool |
| 500 | c = busycode nm |
| 501 | if eq c 0 |
| 502 | ret false |
| 503 | end |
| 504 | answer fd c 0 0 part tlog 0 0 |
| 505 | ret true |
| 506 | end |
| 507 | note busycode: the first 3-digit code in DIR/nm, dropped unless a "+" follows it; 0 when there is none |
| 508 | fn busycode nm view uchar * to ulong |
| 509 | f = fopen nm 0 len nm 524290 0 |
| 510 | if lt f 0 |
| 511 | ret 0 |
| 512 | end |
| 513 | b = new uchar 4096 0 |
| 514 | r = read int f b 0 len b |
| 515 | if lt r 3 |
| 516 | x = fclose int f |
| 517 | ret 0 |
| 518 | end |
| 519 | k = ulong r |
| 520 | c = decat part b 0 3 0 |
| 521 | if not and gt k 3 eq get b 3 43 |
| 522 | w = pwrite int f b 3 sub k 3 0 |
| 523 | t = ftrunc int f long sub k 3 |
| 524 | end |
| 525 | x = fclose int f |
| 526 | ret c |
| 527 | end |
| 528 | note postpt: the patch upload; DIR/busyq first (a 429 carries the write gate's reason line) |
| 529 | fn postpt rq edit uchar * fd int n ulong to void |
| 530 | c = busycode tbusyq |
| 531 | if eq c 429 |
| 532 | answer fd 429 0 0 tptgate |
| 533 | ret |
| 534 | end |
| 535 | if ne c 0 |
| 536 | answer fd c 0 0 part tlog 0 0 |
| 537 | ret |
| 538 | end |
| 539 | savebody rq n tptform |
| 540 | answer fd 201 0 0 tptpath |
| 541 | end |
| 542 | fn origin rq edit uchar * n ulong port ulong to bool |
| 543 | o = new uchar 64 0 |
| 544 | k = ulong 0 |
| 545 | for i ulong len torig |
| 546 | put o i get torig i |
| 547 | end |
| 548 | k = decw port o len torig |
| 549 | cr = new uchar 1 0 |
| 550 | put o k 13 |
| 551 | ret hasi rq n part o 0 add k 1 |
| 552 | end |
| 553 | fn savebody rq edit uchar * n ulong nm view uchar * to void |
| 554 | h = hdrend rq n |
| 555 | appendf nm part rq ulong h sub n ulong h |
| 556 | end |
| 557 | fn status fd int to void |
| 558 | put gst 3 add get gst 3 1 |
| 559 | b = new uchar 4096 0 |
| 560 | if exists trefuse |
| 561 | k = ulong 0 |
| 562 | for i ulong len tj3a |
| 563 | put b i get tj3a i |
| 564 | end |
| 565 | k = len tj3a |
| 566 | f = fopen trefuse 0 len trefuse 524288 0 |
| 567 | r = read int f b k 200 |
| 568 | x = fclose int f |
| 569 | k = add k ulong r |
| 570 | while and gt k 0 eq get b sub k 1 10 bound 4096 |
| 571 | k = sub k 1 |
| 572 | end |
| 573 | for i ulong len tj3b |
| 574 | put b add k i get tj3b i |
| 575 | end |
| 576 | answer fd 200 2 0 part b 0 add k len tj3b |
| 577 | ret |
| 578 | end |
| 579 | if eq get gst 3 1 |
| 580 | answer fd 200 2 0 tj1 |
| 581 | ret |
| 582 | end |
| 583 | answer fd 200 2 0 tj2 |
| 584 | end |
| 585 | note postup: gst 0 = the next seq, 1 = 429 injected, 2 = 409 injected |
| 586 | fn postup rq edit uchar * fd int n ulong to void |
| 587 | h = ulong hdrend rq n |
| 588 | if has rq n tafin |
| 589 | if busyans fd tbusyf |
| 590 | ret |
| 591 | end |
| 592 | postfin rq fd n |
| 593 | ret |
| 594 | end |
| 595 | if busyans fd tbusyp |
| 596 | ret |
| 597 | end |
| 598 | p = bfind part rq h sub n h tseq |
| 599 | if lt p 0 |
| 600 | answer fd 400 0 0 part tlog 0 0 |
| 601 | ret |
| 602 | end |
| 603 | postchk rq fd n ulong p |
| 604 | end |
| 605 | fn postfin rq edit uchar * fd int n ulong to void |
| 606 | h = ulong hdrend rq n |
| 607 | savebody rq n tfin |
| 608 | p = bfind part rq h sub n h tsha |
| 609 | b = new uchar 128 0 |
| 610 | k = ulong 0 |
| 611 | for i ulong len tqueued |
| 612 | put b i get tqueued i |
| 613 | end |
| 614 | k = len tqueued |
| 615 | for i ulong 64 |
| 616 | put b add k i get rq add add add h ulong p len tsha i |
| 617 | end |
| 618 | put b add k 64 10 |
| 619 | answer fd 200 0 0 part b 0 add k 65 |
| 620 | end |
| 621 | fn postchk rq edit uchar * fd int n ulong p ulong to void |
| 622 | h = ulong hdrend rq n |
| 623 | seq = decat rq add add h p len tseq |
| 624 | inj = exists tinject |
| 625 | if and inj and eq seq 1 eq get gst 1 0 |
| 626 | put gst 1 1 |
| 627 | answer fd 429 0 0 part tlog 0 0 |
| 628 | ret |
| 629 | end |
| 630 | if and inj and eq seq 2 eq get gst 2 0 |
| 631 | put gst 2 1 |
| 632 | put gst 0 1 |
| 633 | nextans fd 409 1 |
| 634 | ret |
| 635 | end |
| 636 | if ne seq get gst 0 |
| 637 | nextans fd 409 get gst 0 |
| 638 | ret |
| 639 | end |
| 640 | chunkdata rq h n seq |
| 641 | put gst 0 add seq 1 |
| 642 | nextans fd 200 add seq 1 |
| 643 | end |
| 644 | note chunkdata: up.b64 holds the data of seq 0..seq-1 as one line each; a resent seq rewrites from it |
| 645 | fn chunkdata rq edit uchar * h ulong n ulong seq ulong to void |
| 646 | p = bfind part rq h sub n h tdata |
| 647 | if lt p 0 |
| 648 | exit 5 |
| 649 | end |
| 650 | s = add add h ulong p len tdata |
| 651 | e = s |
| 652 | while lt e n bound 9223372036854775807 |
| 653 | if eq get rq e 38 |
| 654 | break |
| 655 | end |
| 656 | e = add e 1 |
| 657 | end |
| 658 | truncl seq |
| 659 | appendf tupb part rq s sub e s |
| 660 | nl = new uchar 1 0 |
| 661 | put nl 0 10 |
| 662 | appendf tupb nl |
| 663 | end |
| 664 | note truncl: cut up.b64 after its first k lines (a 409 rewinds the client) |
| 665 | fn truncl k ulong to void |
| 666 | f = fopen tupb 0 len tupb 524290 384 |
| 667 | if lt f 0 |
| 668 | ret |
| 669 | end |
| 670 | b = new uchar 65536 0 |
| 671 | seen = ulong 0 |
| 672 | pos = ulong 0 |
| 673 | while lt seen k bound 9223372036854775807 |
| 674 | r = read int f b 0 len b |
| 675 | if le r 0 |
| 676 | break |
| 677 | end |
| 678 | for i ulong ulong r bound 65536 |
| 679 | if lt seen k |
| 680 | pos = add pos 1 |
| 681 | if eq get b i 10 |
| 682 | seen = add seen 1 |
| 683 | end |
| 684 | end |
| 685 | end |
| 686 | end |
| 687 | x = ftrunc int f long pos |
| 688 | x = fclose int f |
| 689 | end |
| 690 | fn nextans fd int code ulong v ulong to void |
| 691 | b = new uchar 40 0 |
| 692 | k = ulong 0 |
| 693 | for i ulong len tnext |
| 694 | put b i get tnext i |
| 695 | end |
| 696 | k = decw v b len tnext |
| 697 | put b k 10 |
| 698 | answer fd code 0 0 part b 0 add k 1 |
| 699 | end |
| 700 | fn main to int |
| 701 | if ne argcnt 4 |
| 702 | x = write 2 tusage 0 len tusage |
| 703 | ret 2 |
| 704 | end |
| 705 | m = new uchar 16 0 |
| 706 | mn = argread 1 m 0 16 |
| 707 | pb = new uchar 16 0 |
| 708 | pn = argread 2 pb 0 16 |
| 709 | port = decat part pb 0 ulong pn 0 |
| 710 | d = new uchar 4096 0 |
| 711 | dn = argread 3 d 0 4096 |
| 712 | if lt chdir d 0 ulong dn 0 |
| 713 | ret 2 |
| 714 | end |
| 715 | s = socknew 2 1 0 |
| 716 | one = new uchar 4 0 |
| 717 | put one 0 1 |
| 718 | x = sockopt int s 1 2 one 0 4 |
| 719 | sa = new uchar 16 0 |
| 720 | put sa 0 2 |
| 721 | put sa 2 uchar shr port 8 |
| 722 | put sa 3 uchar and port 255 |
| 723 | put sa 4 127 |
| 724 | put sa 7 1 |
| 725 | if lt bind int s sa 0 16 0 |
| 726 | ret 3 |
| 727 | end |
| 728 | if lt listen int s 64 0 |
| 729 | ret 3 |
| 730 | end |
| 731 | counting = and eq mn 5 bat m 0 tcount |
| 732 | while true bound 9223372036854775807 |
| 733 | c = accept int s 0 |
| 734 | if lt c 0 |
| 735 | continue |
| 736 | end |
| 737 | appendf tconns tconn |
| 738 | if not counting |
| 739 | serve1 int c port |
| 740 | end |
| 741 | x = fclose int c |
| 742 | end |
| 743 | ret 0 |
| 744 | end |
| 745 | note serve1: one request of at most 1 MiB (a chunk form is about 60 KB); larger ones are not read |
| 746 | fn serve1 c int port ulong to void |
| 747 | rq = new uchar 1048576 0 |
| 748 | n = readreq rq c |
| 749 | if gt n 0 |
| 750 | logline rq n |
| 751 | route rq c n port |
| 752 | end |
| 753 | end |