;; curl-wasm — A curl clone written in plain WebAssembly Text format (WAT) ;; Targets the WASI 0.2 Component Model (wasi:cli, wasi:io, wasi:clocks, wasi:filesystem, wasi:http). ;; Assemble & validate with: ;; wasm-tools parse curl.wat -o curl.wasm ;; wasm-tools validate curl.wasm --features component-model ;; Run with: ;; wasmtime run -S http --dir=. -- curl.wasm [options...] ... (component ;; ============================================================================ ;; 1. WASI 0.2 Component Imports ;; ============================================================================ ;; --- wasi:io/error@0.2.0 --- (import "wasi:io/error@0.2.0" (instance $io_error (export "error" (type $err (sub resource))) (export "[method]error.to-debug-string" (func (param "self" (borrow $err)) (result string))) )) (alias export $io_error "error" (type $io-error)) ;; --- wasi:io/poll@0.2.0 --- (import "wasi:io/poll@0.2.0" (instance $io_poll (export "pollable" (type $p (sub resource))) (export "[method]pollable.block" (func (param "self" (borrow $p)))) )) (alias export $io_poll "pollable" (type $pollable)) ;; --- wasi:io/streams@0.2.0 --- (import "wasi:io/streams@0.2.0" (instance $io_streams (export "error" (type $e (eq $io-error))) (export "pollable" (type $p (eq $pollable))) (type $se_def (variant (case "last-operation-failed" (own $e)) (case "closed"))) (export "stream-error" (type $se (eq $se_def))) (export "input-stream" (type $is (sub resource))) (export "output-stream" (type $os (sub resource))) (export "[method]input-stream.blocking-read" (func (param "self" (borrow $is)) (param "len" u64) (result (result (list u8) (error $se))))) (export "[method]output-stream.check-write" (func (param "self" (borrow $os)) (result (result u64 (error $se))))) (export "[method]output-stream.write" (func (param "self" (borrow $os)) (param "contents" (list u8)) (result (result (error $se))))) (export "[method]output-stream.blocking-flush" (func (param "self" (borrow $os)) (result (result (error $se))))) (export "[method]output-stream.blocking-write-and-flush" (func (param "self" (borrow $os)) (param "contents" (list u8)) (result (result (error $se))))) (export "[method]output-stream.subscribe" (func (param "self" (borrow $os)) (result (own $p)))) )) (alias export $io_streams "input-stream" (type $input-stream)) (alias export $io_streams "output-stream" (type $output-stream)) ;; --- wasi:clocks/monotonic-clock@0.2.0 --- (import "wasi:clocks/monotonic-clock@0.2.0" (instance $mclock (type $dur_def u64) (export "duration" (type $dur (eq $dur_def))) (type $inst_def u64) (export "instant" (type $inst (eq $inst_def))) (export "now" (func (result $inst))) )) (alias export $mclock "duration" (type $duration)) ;; --- wasi:cli/*@0.2.0 --- (import "wasi:cli/environment@0.2.0" (instance $cli_env (export "get-arguments" (func (result (list string)))) )) (import "wasi:cli/stdin@0.2.0" (instance $cli_stdin (export "input-stream" (type $is (eq $input-stream))) (export "get-stdin" (func (result (own $is)))) )) (import "wasi:cli/stdout@0.2.0" (instance $cli_stdout (export "output-stream" (type $os (eq $output-stream))) (export "get-stdout" (func (result (own $os)))) )) (import "wasi:cli/stderr@0.2.0" (instance $cli_stderr (export "output-stream" (type $os (eq $output-stream))) (export "get-stderr" (func (result (own $os)))) )) ;; --- wasi:filesystem/types@0.2.0 --- (import "wasi:filesystem/types@0.2.0" (instance $fs_types (export "input-stream" (type $is (eq $input-stream))) (export "output-stream" (type $os (eq $output-stream))) (type $filesize_def u64) (export "filesize" (type $filesize (eq $filesize_def))) (type $pf_def (flags "symlink-follow")) (export "path-flags" (type $path-flags (eq $pf_def))) (type $of_def (flags "create" "directory" "exclusive" "truncate")) (export "open-flags" (type $open-flags (eq $of_def))) (type $df_def (flags "read" "write" "file-integrity-sync" "data-integrity-sync" "requested-write-sync" "mutate-directory")) (export "descriptor-flags" (type $descriptor-flags (eq $df_def))) (type $ec_def (enum "access" "would-block" "already" "bad-descriptor" "busy" "deadlock" "quota" "exist" "file-too-large" "illegal-byte-sequence" "in-progress" "interrupted" "invalid" "io" "is-directory" "loop" "too-many-links" "message-size" "name-too-long" "no-device" "no-entry" "no-lock" "insufficient-memory" "insufficient-space" "not-directory" "not-empty" "not-recoverable" "unsupported" "no-tty" "no-such-device" "overflow" "not-permitted" "pipe" "read-only" "invalid-seek" "text-file-busy" "cross-device" )) (export "error-code" (type $fs-error-code (eq $ec_def))) (export "descriptor" (type $desc (sub resource))) (export "[method]descriptor.read-via-stream" (func (param "self" (borrow $desc)) (param "offset" $filesize) (result (result (own $is) (error $fs-error-code))))) (export "[method]descriptor.write-via-stream" (func (param "self" (borrow $desc)) (param "offset" $filesize) (result (result (own $os) (error $fs-error-code))))) (export "[method]descriptor.open-at" (func (param "self" (borrow $desc)) (param "path-flags" $path-flags) (param "path" string) (param "open-flags" $open-flags) (param "flags" $descriptor-flags) (result (result (own $desc) (error $fs-error-code))))) )) (alias export $fs_types "descriptor" (type $descriptor)) ;; --- wasi:filesystem/preopens@0.2.0 --- (import "wasi:filesystem/preopens@0.2.0" (instance $fs_preopens (export "descriptor" (type $desc (eq $descriptor))) (export "get-directories" (func (result (list (tuple (own $desc) string))))) )) ;; --- wasi:http/types@0.2.0 --- (import "wasi:http/types@0.2.0" (instance $http_types (export "duration" (type $dur (eq $duration))) (export "input-stream" (type $is (eq $input-stream))) (export "output-stream" (type $os (eq $output-stream))) (export "pollable" (type $p (eq $pollable))) (type $method_def (variant (case "get") (case "head") (case "post") (case "put") (case "delete") (case "connect") (case "options") (case "trace") (case "patch") (case "other" string) )) (export "method" (type $method (eq $method_def))) (type $scheme_def (variant (case "HTTP") (case "HTTPS") (case "other" string) )) (export "scheme" (type $scheme (eq $scheme_def))) (type $dns_err_def (record (field "rcode" (option string)) (field "info-code" (option u16)) )) (export "DNS-error-payload" (type $dns-err (eq $dns_err_def))) (type $tls_err_def (record (field "alert-id" (option u8)) (field "alert-message" (option string)) )) (export "TLS-alert-received-payload" (type $tls-err (eq $tls_err_def))) (type $fsz_err_def (record (field "field-name" (option string)) (field "field-size" (option u32)) )) (export "field-size-payload" (type $fsz-err (eq $fsz_err_def))) (type $http_ec_def (variant (case "DNS-timeout") (case "DNS-error" $dns-err) (case "destination-not-found") (case "destination-unavailable") (case "destination-IP-prohibited") (case "destination-IP-unroutable") (case "connection-refused") (case "connection-terminated") (case "connection-timeout") (case "connection-read-timeout") (case "connection-write-timeout") (case "connection-limit-reached") (case "TLS-protocol-error") (case "TLS-certificate-error") (case "TLS-alert-received" $tls-err) (case "HTTP-request-denied") (case "HTTP-request-length-required") (case "HTTP-request-body-size" (option u64)) (case "HTTP-request-method-invalid") (case "HTTP-request-URI-invalid") (case "HTTP-request-URI-too-long") (case "HTTP-request-header-section-size" (option u32)) (case "HTTP-request-header-size" (option $fsz-err)) (case "HTTP-request-trailer-section-size" (option u32)) (case "HTTP-request-trailer-size" $fsz-err) (case "HTTP-response-incomplete") (case "HTTP-response-header-section-size" (option u32)) (case "HTTP-response-header-size" $fsz-err) (case "HTTP-response-body-size" (option u64)) (case "HTTP-response-trailer-section-size" (option u32)) (case "HTTP-response-trailer-size" $fsz-err) (case "HTTP-response-transfer-coding" (option string)) (case "HTTP-response-content-coding" (option string)) (case "HTTP-response-timeout") (case "HTTP-upgrade-failed") (case "HTTP-protocol-error") (case "loop-detected") (case "configuration-error") (case "internal-error" (option string)) )) (export "error-code" (type $http-error-code (eq $http_ec_def))) (type $hdr_err_def (variant (case "invalid-syntax") (case "forbidden") (case "immutable") )) (export "header-error" (type $header-error (eq $hdr_err_def))) (type $fk_def string) (export "field-key" (type $field-key (eq $fk_def))) (type $fv_def (list u8)) (export "field-value" (type $field-value (eq $fv_def))) (export "fields" (type $fields (sub resource))) (export "[constructor]fields" (func (result (own $fields)))) (export "[method]fields.append" (func (param "self" (borrow $fields)) (param "name" $field-key) (param "value" $field-value) (result (result (error $header-error))))) (export "[method]fields.set" (func (param "self" (borrow $fields)) (param "name" $field-key) (param "value" (list $field-value)) (result (result (error $header-error))))) (export "[method]fields.delete" (func (param "self" (borrow $fields)) (param "name" $field-key) (result (result (error $header-error))))) (export "[method]fields.entries" (func (param "self" (borrow $fields)) (result (list (tuple $field-key $field-value))))) (export "headers" (type $headers (eq $fields))) (export "trailers" (type $trailers (eq $fields))) (type $status_def u16) (export "status-code" (type $status-code (eq $status_def))) (export "outgoing-body" (type $outgoing-body (sub resource))) (export "[method]outgoing-body.write" (func (param "self" (borrow $outgoing-body)) (result (result (own $os))))) (export "[static]outgoing-body.finish" (func (param "this" (own $outgoing-body)) (param "trailers" (option (own $trailers))) (result (result (error $http-error-code))))) (export "outgoing-request" (type $outgoing-request (sub resource))) (export "[constructor]outgoing-request" (func (param "headers" (own $headers)) (result (own $outgoing-request)))) (export "[method]outgoing-request.body" (func (param "self" (borrow $outgoing-request)) (result (result (own $outgoing-body))))) (export "[method]outgoing-request.set-method" (func (param "self" (borrow $outgoing-request)) (param "method" $method) (result (result)))) (export "[method]outgoing-request.set-path-with-query" (func (param "self" (borrow $outgoing-request)) (param "path-with-query" (option string)) (result (result)))) (export "[method]outgoing-request.set-scheme" (func (param "self" (borrow $outgoing-request)) (param "scheme" (option $scheme)) (result (result)))) (export "[method]outgoing-request.set-authority" (func (param "self" (borrow $outgoing-request)) (param "authority" (option string)) (result (result)))) (export "request-options" (type $request-options (sub resource))) (export "[constructor]request-options" (func (result (own $request-options)))) (export "[method]request-options.set-connect-timeout" (func (param "self" (borrow $request-options)) (param "duration" (option $dur)) (result (result)))) (export "[method]request-options.set-first-byte-timeout" (func (param "self" (borrow $request-options)) (param "duration" (option $dur)) (result (result)))) (export "[method]request-options.set-between-bytes-timeout" (func (param "self" (borrow $request-options)) (param "duration" (option $dur)) (result (result)))) (export "incoming-body" (type $incoming-body (sub resource))) (export "[method]incoming-body.stream" (func (param "self" (borrow $incoming-body)) (result (result (own $is))))) (export "incoming-response" (type $incoming-response (sub resource))) (export "[method]incoming-response.status" (func (param "self" (borrow $incoming-response)) (result $status-code))) (export "[method]incoming-response.headers" (func (param "self" (borrow $incoming-response)) (result (own $headers)))) (export "[method]incoming-response.consume" (func (param "self" (borrow $incoming-response)) (result (result (own $incoming-body))))) (export "future-incoming-response" (type $fir (sub resource))) (export "[method]future-incoming-response.subscribe" (func (param "self" (borrow $fir)) (result (own $p)))) (export "[method]future-incoming-response.get" (func (param "self" (borrow $fir)) (result (option (result (result (own $incoming-response) (error $http-error-code))))))) )) (alias export $http_types "fields" (type $fields)) (alias export $http_types "outgoing-body" (type $outgoing-body)) (alias export $http_types "outgoing-request" (type $outgoing-request)) (alias export $http_types "request-options" (type $request-options)) (alias export $http_types "incoming-body" (type $incoming-body)) (alias export $http_types "incoming-response" (type $incoming-response)) (alias export $http_types "future-incoming-response" (type $future-incoming-response)) (alias export $http_types "error-code" (type $http-error-code)) ;; --- wasi:http/outgoing-handler@0.2.0 --- (import "wasi:http/outgoing-handler@0.2.0" (instance $http_out (export "outgoing-request" (type $or (eq $outgoing-request))) (export "request-options" (type $ro (eq $request-options))) (export "future-incoming-response" (type $fir (eq $future-incoming-response))) (export "error-code" (type $ec (eq $http-error-code))) (export "handle" (func (param "request" (own $or)) (param "options" (option (own $ro))) (result (result (own $fir) (error $ec))))) )) ;; ============================================================================ ;; 2. Memory & Bump/Mark Allocator Core Module ;; ============================================================================ (core module $mem_mod (memory (export "memory") 16) (global $heap (mut i32) (i32.const 65536)) ;; 0x10000 (func $realloc (export "realloc") (param $old_ptr i32) (param $old_size i32) (param $align i32) (param $new_size i32) (result i32) (local $a i32) (local $ptr i32) (local $end i32) (local $cap i32) (local $pages i32) (local $copy_len i32) (local.set $a (local.get $align)) (if (i32.eq (local.get $a) (i32.const 0)) (then (local.set $a (i32.const 1))) ) (local.set $ptr (i32.and (i32.add (global.get $heap) (i32.sub (local.get $a) (i32.const 1))) (i32.xor (i32.sub (local.get $a) (i32.const 1)) (i32.const -1)) ) ) (if (i32.eq (local.get $new_size) (i32.const 0)) (then (global.set $heap (local.get $ptr)) (return (local.get $ptr)) ) ) (local.set $end (i32.add (local.get $ptr) (local.get $new_size))) (local.set $cap (i32.shl (memory.size) (i32.const 16))) (if (i32.gt_u (local.get $end) (local.get $cap)) (then (local.set $pages (i32.shr_u (i32.add (i32.sub (local.get $end) (local.get $cap)) (i32.const 65535)) (i32.const 16) ) ) (if (i32.eq (memory.grow (local.get $pages)) (i32.const -1)) (then (unreachable)) ) ) ) (global.set $heap (local.get $end)) (if (i32.and (i32.ne (local.get $old_ptr) (i32.const 0)) (i32.ne (local.get $old_size) (i32.const 0))) (then (local.set $copy_len (select (local.get $old_size) (local.get $new_size) (i32.lt_u (local.get $old_size) (local.get $new_size))) ) (memory.copy (local.get $ptr) (local.get $old_ptr) (local.get $copy_len)) ) ) (local.get $ptr) ) (func (export "alloc") (param $size i32) (param $align i32) (result i32) (call $realloc (i32.const 0) (i32.const 0) (local.get $align) (local.get $size)) ) (func (export "heap_mark") (result i32) (global.get $heap) ) (func (export "heap_reset") (param $mark i32) (global.set $heap (local.get $mark)) ) ) (core instance $mem_inst (instantiate $mem_mod)) (alias core export $mem_inst "memory" (core memory $mem)) (alias core export $mem_inst "realloc" (core func $realloc)) (alias core export $mem_inst "alloc" (core func $alloc)) (alias core export $mem_inst "heap_mark" (core func $heap_mark)) (alias core export $mem_inst "heap_reset" (core func $heap_reset)) ;; ============================================================================ ;; 3. Canonical ABI Lowerings & Resource Drops ;; ============================================================================ ;; wasi:io/error (alias export $io_error "[method]error.to-debug-string" (func $c_err_str)) (core func $err_to_debug_string (canon lower (func $c_err_str) (memory $mem) (realloc $realloc))) ;; wasi:io/poll (alias export $io_poll "[method]pollable.block" (func $c_poll_block)) (core func $poll_block (canon lower (func $c_poll_block))) ;; wasi:io/streams (alias export $io_streams "[method]input-stream.blocking-read" (func $c_in_read)) (core func $in_blocking_read (canon lower (func $c_in_read) (memory $mem) (realloc $realloc))) (alias export $io_streams "[method]output-stream.check-write" (func $c_out_cw)) (core func $out_check_write (canon lower (func $c_out_cw) (memory $mem))) (alias export $io_streams "[method]output-stream.write" (func $c_out_w)) (core func $out_write (canon lower (func $c_out_w) (memory $mem))) (alias export $io_streams "[method]output-stream.blocking-flush" (func $c_out_bf)) (core func $out_blocking_flush (canon lower (func $c_out_bf) (memory $mem))) (alias export $io_streams "[method]output-stream.subscribe" (func $c_out_sub)) (core func $out_subscribe (canon lower (func $c_out_sub))) ;; wasi:clocks/monotonic-clock (alias export $mclock "now" (func $c_clock_now)) (core func $clock_now (canon lower (func $c_clock_now))) ;; wasi:cli/* (alias export $cli_env "get-arguments" (func $c_get_args)) (core func $get_arguments (canon lower (func $c_get_args) (memory $mem) (realloc $realloc))) (alias export $cli_stdin "get-stdin" (func $c_get_stdin)) (core func $get_stdin (canon lower (func $c_get_stdin))) (alias export $cli_stdout "get-stdout" (func $c_get_stdout)) (core func $get_stdout (canon lower (func $c_get_stdout))) (alias export $cli_stderr "get-stderr" (func $c_get_stderr)) (core func $get_stderr (canon lower (func $c_get_stderr))) ;; wasi:filesystem/* (alias export $fs_preopens "get-directories" (func $c_get_dirs)) (core func $get_directories (canon lower (func $c_get_dirs) (memory $mem) (realloc $realloc))) (alias export $fs_types "[method]descriptor.open-at" (func $c_open_at)) (core func $desc_open_at (canon lower (func $c_open_at) (memory $mem))) (alias export $fs_types "[method]descriptor.read-via-stream" (func $c_read_via)) (core func $desc_read_via_stream (canon lower (func $c_read_via) (memory $mem))) (alias export $fs_types "[method]descriptor.write-via-stream" (func $c_write_via)) (core func $desc_write_via_stream (canon lower (func $c_write_via) (memory $mem))) ;; wasi:http/types (alias export $http_types "[constructor]fields" (func $c_new_fields)) (core func $new_fields (canon lower (func $c_new_fields))) (alias export $http_types "[method]fields.append" (func $c_f_append)) (core func $fields_append (canon lower (func $c_f_append) (memory $mem))) (alias export $http_types "[method]fields.set" (func $c_f_set)) (core func $fields_set (canon lower (func $c_f_set) (memory $mem))) (alias export $http_types "[method]fields.delete" (func $c_f_del)) (core func $fields_delete (canon lower (func $c_f_del) (memory $mem))) (alias export $http_types "[method]fields.entries" (func $c_f_ent)) (core func $fields_entries (canon lower (func $c_f_ent) (memory $mem) (realloc $realloc))) (alias export $http_types "[constructor]outgoing-request" (func $c_new_req)) (core func $new_outgoing_request (canon lower (func $c_new_req))) (alias export $http_types "[method]outgoing-request.body" (func $c_req_body)) (core func $outgoing_request_body (canon lower (func $c_req_body) (memory $mem))) (alias export $http_types "[method]outgoing-request.set-method" (func $c_set_m)) (core func $req_set_method (canon lower (func $c_set_m) (memory $mem))) (alias export $http_types "[method]outgoing-request.set-path-with-query" (func $c_set_p)) (core func $req_set_path (canon lower (func $c_set_p) (memory $mem))) (alias export $http_types "[method]outgoing-request.set-scheme" (func $c_set_s)) (core func $req_set_scheme (canon lower (func $c_set_s) (memory $mem))) (alias export $http_types "[method]outgoing-request.set-authority" (func $c_set_a)) (core func $req_set_authority (canon lower (func $c_set_a) (memory $mem))) (alias export $http_types "[method]outgoing-body.write" (func $c_ob_w)) (core func $outgoing_body_write (canon lower (func $c_ob_w) (memory $mem))) (alias export $http_types "[static]outgoing-body.finish" (func $c_ob_f)) (core func $outgoing_body_finish (canon lower (func $c_ob_f) (memory $mem) (realloc $realloc))) (alias export $http_types "[constructor]request-options" (func $c_new_ro)) (core func $new_request_options (canon lower (func $c_new_ro))) (alias export $http_types "[method]request-options.set-connect-timeout" (func $c_ro_ct)) (core func $ro_set_connect_timeout (canon lower (func $c_ro_ct))) (alias export $http_types "[method]request-options.set-first-byte-timeout" (func $c_ro_fb)) (core func $ro_set_first_byte_timeout (canon lower (func $c_ro_fb))) (alias export $http_types "[method]request-options.set-between-bytes-timeout" (func $c_ro_bb)) (core func $ro_set_between_bytes_timeout (canon lower (func $c_ro_bb))) (alias export $http_types "[method]future-incoming-response.subscribe" (func $c_fir_sub)) (core func $fir_subscribe (canon lower (func $c_fir_sub))) (alias export $http_types "[method]future-incoming-response.get" (func $c_fir_get)) (core func $fir_get (canon lower (func $c_fir_get) (memory $mem) (realloc $realloc))) (alias export $http_types "[method]incoming-response.status" (func $c_ir_st)) (core func $resp_status (canon lower (func $c_ir_st))) (alias export $http_types "[method]incoming-response.headers" (func $c_ir_hdr)) (core func $resp_headers (canon lower (func $c_ir_hdr))) (alias export $http_types "[method]incoming-response.consume" (func $c_ir_con)) (core func $resp_consume (canon lower (func $c_ir_con) (memory $mem))) (alias export $http_types "[method]incoming-body.stream" (func $c_ib_str)) (core func $body_stream (canon lower (func $c_ib_str) (memory $mem))) ;; wasi:http/outgoing-handler (alias export $http_out "handle" (func $c_http_handle)) (core func $http_handle (canon lower (func $c_http_handle) (memory $mem) (realloc $realloc))) ;; Resource drops (core func $drop_io_error (canon resource.drop $io-error)) (core func $drop_pollable (canon resource.drop $pollable)) (core func $drop_input_stream (canon resource.drop $input-stream)) (core func $drop_output_stream (canon resource.drop $output-stream)) (core func $drop_descriptor (canon resource.drop $descriptor)) (core func $drop_fields (canon resource.drop $fields)) (core func $drop_outgoing_body (canon resource.drop $outgoing-body)) (core func $drop_outgoing_request (canon resource.drop $outgoing-request)) (core func $drop_request_options (canon resource.drop $request-options)) (core func $drop_incoming_body (canon resource.drop $incoming-body)) (core func $drop_incoming_response (canon resource.drop $incoming-response)) (core func $drop_fir (canon resource.drop $future-incoming-response)) ;; ============================================================================ ;; 4. Core curl Implementation Module ;; ============================================================================ (core module $curl ;; Memory & Allocator (import "mem" "memory" (memory 16)) (import "mem" "realloc" (func $realloc (param i32 i32 i32 i32) (result i32))) (import "mem" "alloc" (func $alloc (param i32 i32) (result i32))) (import "mem" "heap_mark" (func $heap_mark (result i32))) (import "mem" "heap_reset" (func $heap_reset (param i32))) ;; WASI Imports (import "wasi" "err_to_debug_string" (func $err_to_debug_string (param i32 i32))) (import "wasi" "poll_block" (func $poll_block (param i32))) (import "wasi" "in_blocking_read" (func $in_blocking_read (param i32 i64 i32))) (import "wasi" "out_check_write" (func $out_check_write (param i32 i32))) (import "wasi" "out_write" (func $out_write (param i32 i32 i32 i32))) (import "wasi" "out_blocking_flush" (func $out_blocking_flush (param i32 i32))) (import "wasi" "out_subscribe" (func $out_subscribe (param i32) (result i32))) (import "wasi" "clock_now" (func $clock_now (result i64))) (import "wasi" "get_arguments" (func $get_arguments (param i32))) (import "wasi" "get_stdin" (func $get_stdin (result i32))) (import "wasi" "get_stdout" (func $get_stdout (result i32))) (import "wasi" "get_stderr" (func $get_stderr (result i32))) (import "wasi" "get_directories" (func $get_directories (param i32))) (import "wasi" "desc_open_at" (func $desc_open_at (param i32 i32 i32 i32 i32 i32 i32))) (import "wasi" "desc_read_via_stream" (func $desc_read_via_stream (param i32 i64 i32))) (import "wasi" "desc_write_via_stream" (func $desc_write_via_stream (param i32 i64 i32))) (import "wasi" "new_fields" (func $new_fields (result i32))) (import "wasi" "fields_append" (func $fields_append (param i32 i32 i32 i32 i32 i32))) (import "wasi" "fields_set" (func $fields_set (param i32 i32 i32 i32 i32 i32))) (import "wasi" "fields_delete" (func $fields_delete (param i32 i32 i32 i32))) (import "wasi" "fields_entries" (func $fields_entries (param i32 i32))) (import "wasi" "new_outgoing_request" (func $new_outgoing_request (param i32) (result i32))) (import "wasi" "outgoing_request_body" (func $outgoing_request_body (param i32 i32))) (import "wasi" "req_set_method" (func $req_set_method (param i32 i32 i32 i32) (result i32))) (import "wasi" "req_set_path" (func $req_set_path (param i32 i32 i32 i32) (result i32))) (import "wasi" "req_set_scheme" (func $req_set_scheme (param i32 i32 i32 i32 i32) (result i32))) (import "wasi" "req_set_authority" (func $req_set_authority (param i32 i32 i32 i32) (result i32))) (import "wasi" "outgoing_body_write" (func $outgoing_body_write (param i32 i32))) (import "wasi" "outgoing_body_finish" (func $outgoing_body_finish (param i32 i32 i32 i32))) (import "wasi" "new_request_options" (func $new_request_options (result i32))) (import "wasi" "ro_set_connect_timeout" (func $ro_set_connect_timeout (param i32 i32 i64) (result i32))) (import "wasi" "ro_set_first_byte_timeout" (func $ro_set_first_byte_timeout (param i32 i32 i64) (result i32))) (import "wasi" "ro_set_between_bytes_timeout" (func $ro_set_between_bytes_timeout (param i32 i32 i64) (result i32))) (import "wasi" "fir_subscribe" (func $fir_subscribe (param i32) (result i32))) (import "wasi" "fir_get" (func $fir_get (param i32 i32))) (import "wasi" "resp_status" (func $resp_status (param i32) (result i32))) (import "wasi" "resp_headers" (func $resp_headers (param i32) (result i32))) (import "wasi" "resp_consume" (func $resp_consume (param i32 i32))) (import "wasi" "body_stream" (func $body_stream (param i32 i32))) (import "wasi" "http_handle" (func $http_handle (param i32 i32 i32 i32))) ;; Resource Drops (import "wasi" "drop_io_error" (func $drop_io_error (param i32))) (import "wasi" "drop_pollable" (func $drop_pollable (param i32))) (import "wasi" "drop_input_stream" (func $drop_input_stream (param i32))) (import "wasi" "drop_output_stream" (func $drop_output_stream (param i32))) (import "wasi" "drop_descriptor" (func $drop_descriptor (param i32))) (import "wasi" "drop_fields" (func $drop_fields (param i32))) (import "wasi" "drop_outgoing_body" (func $drop_outgoing_body (param i32))) (import "wasi" "drop_outgoing_request" (func $drop_outgoing_request (param i32))) (import "wasi" "drop_request_options" (func $drop_request_options (param i32))) (import "wasi" "drop_incoming_body" (func $drop_incoming_body (param i32))) (import "wasi" "drop_incoming_response" (func $drop_incoming_response (param i32))) (import "wasi" "drop_fir" (func $drop_fir (param i32))) ;; ========================================================================== ;; Static Data Segments (0x0000 .. 0x6FFF, strictly non-overlapping) ;; ========================================================================== (data (i32.const 0x0000) "Usage: curl-wasm [options...] ...\n" " -A, --user-agent Send User-Agent to server\n" " -b, --cookie Send cookies from string\n" " --connect-timeout Maximum time allowed for connection\n" " -d, --data HTTP POST data (@file strips CR/LF)\n" " --data-ascii HTTP POST ASCII data\n" " --data-binary HTTP POST binary data (@file raw)\n" " --data-raw HTTP POST data, '@' allowed literally\n" " --data-urlencode HTTP POST data url encoded\n" " -D, --dump-header Write the received headers to \n" " -e, --referer Referrer URL\n" " -f, --fail Fail silently (no output) on HTTP errors\n" " -G, --get Put the post data in the URL and use GET\n" " -H, --header
Pass custom header(s) to server\n" " -h, --help This help text\n" " -i, --include Include protocol response headers in output\n" " -I, --head Show document info only (HEAD request)\n" " -L, --location Follow redirects\n" " -m, --max-time Maximum time allowed for transfer\n" " --max-redirs Maximum number of redirects allowed\n" " -o, --output Write to file instead of stdout\n" " -O, --remote-name Write output to file named as remote file\n" " -r, --range Retrieve only the bytes within RANGE\n" " -s, --silent Silent mode\n" " -S, --show-error Show error even when -s is used\n" " -T, --upload-file Transfer local FILE to destination\n" " -u, --user Server user and password\n" " -v, --verbose Make the operation more talkative\n" " -V, --version Show version number and quit\n" " -w, --write-out Use output FORMAT after completion\n" " -X, --request Specify request method to use\n" ) ;; len = 2070 (0x0000 .. 0x0816) (data (i32.const 0x0900) "curl-wasm 0.1.0 (wasm32-wasi-0.2) libwasi-http\n" "Protocols: http https\n" "Features: HTTPS Largefile TLS\n" ) ;; len = 99 (0x0900 .. 0x0963) ;; Short literals (0x0A00 .. 0x0DFF) (data (i32.const 0x0A00) "\n") ;; 1 (data (i32.const 0x0A04) "\r\n") ;; 2 (data (i32.const 0x0A08) ": ") ;; 2 (data (i32.const 0x0A0C) " ") ;; 1 (data (i32.const 0x0A10) "/") ;; 1 (data (i32.const 0x0A14) "?") ;; 1 (data (i32.const 0x0A18) "&") ;; 1 (data (i32.const 0x0A1C) "=") ;; 1 (data (i32.const 0x0A20) "0") ;; 1 (data (i32.const 0x0A24) "-") ;; 1 (data (i32.const 0x0A28) ".") ;; 1 (data (i32.const 0x0A2C) "..") ;; 2 (data (i32.const 0x0A30) "./") ;; 2 (data (i32.const 0x0A34) "://") ;; 3 (data (i32.const 0x0A38) "/dev/null") ;; 9 (data (i32.const 0x0A50) "HTTP") ;; 4 (data (i32.const 0x0A58) "HTTPS") ;; 5 (data (i32.const 0x0A60) "http") ;; 4 (data (i32.const 0x0A68) "https") ;; 5 (data (i32.const 0x0A70) "http://") ;; 7 (data (i32.const 0x0A78) "https://") ;; 8 (data (i32.const 0x0A90) "GET") ;; 3 (data (i32.const 0x0A98) "HEAD") ;; 4 (data (i32.const 0x0AA0) "POST") ;; 4 (data (i32.const 0x0AA8) "PUT") ;; 3 (data (i32.const 0x0AB0) "DELETE") ;; 6 (data (i32.const 0x0AB8) "CONNECT") ;; 7 (data (i32.const 0x0AC0) "OPTIONS") ;; 7 (data (i32.const 0x0AC8) "TRACE") ;; 5 (data (i32.const 0x0AD0) "PATCH") ;; 5 (data (i32.const 0x0AE0) "User-Agent") ;; 10 (data (i32.const 0x0AF0) "curl-wasm/0.1.0") ;; 15 (data (i32.const 0x0B08) "Accept") ;; 6 (data (i32.const 0x0B10) "*/*") ;; 3 (data (i32.const 0x0B18) "Authorization") ;; 13 (data (i32.const 0x0B28) "Basic ") ;; 6 (data (i32.const 0x0B30) "Referer") ;; 7 (data (i32.const 0x0B38) "Cookie") ;; 6 (data (i32.const 0x0B40) "Range") ;; 5 (data (i32.const 0x0B48) "bytes=") ;; 6 (data (i32.const 0x0B50) "Content-Type") ;; 12 (data (i32.const 0x0B60) "application/x-www-form-urlencoded") ;; 33 (data (i32.const 0x0B88) "Content-Length") ;; 14 (data (i32.const 0x0B98) "Host") ;; 4 (data (i32.const 0x0BA0) "location") ;; 8 (data (i32.const 0x0BB0) "content-type") ;; 12 (data (i32.const 0x0BC0) "HTTP/1.1 ") ;; 9 (data (i32.const 0x0BD0) " HTTP/1.1\n") ;; 10 (data (i32.const 0x0BE0) "> ") ;; 2 (data (i32.const 0x0BE4) "< ") ;; 2 (data (i32.const 0x0BE8) "* ") ;; 2 (data (i32.const 0x0BEC) ">\n") ;; 2 (data (i32.const 0x0BF0) "<\r\n") ;; 3 (data (i32.const 0x0C00) "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") ;; 64 (data (i32.const 0x0C50) "0123456789ABCDEF") ;; 16 ;; CLI & Runtime Error Messages (0x0D00 .. 0x15FF) (data (i32.const 0x0D00) "curl: ") ;; 6 (data (i32.const 0x0D08) "no URL specified!\ncurl: try 'curl-wasm --help' for more information\n") ;; 67 (data (i32.const 0x0D50) "option ") ;; 7 (data (i32.const 0x0D58) ": is unknown\n") ;; 13 (data (i32.const 0x0D68) ": requires parameter\n") ;; 21 (data (i32.const 0x0D80) "(3) URL rejected: Malformed input to a URL function\n") ;; 52 (data (i32.const 0x0DB8) "(22) The requested URL returned error: ") ;; 39 (data (i32.const 0x0DE0) "(47) Maximum (") ;; 14 (data (i32.const 0x0DF0) ") redirects followed\n") ;; 21 (data (i32.const 0x0E08) "(26) Failed to open/read local data from file/application: ") ;; 58 (data (i32.const 0x0E48) "(23) Failed writing received data to disk/application: ") ;; 54 (data (i32.const 0x0E80) "(23) Remote file name has no length!\n") ;; 37 (data (i32.const 0x0EA8) "(28) Operation timed out\n") ;; 25 (data (i32.const 0x0EC8) "(6) Could not resolve host: ") ;; 28 (data (i32.const 0x0EE8) "(7) Failed to connect to host: ") ;; 31 (data (i32.const 0x0F08) "(35) TLS connect error: ") ;; 24 (data (i32.const 0x0F20) "(52) Empty reply from server / incomplete response\n") ;; 51 (data (i32.const 0x0F58) "(56) Failure with receiving network data: ") ;; 42 (data (i32.const 0x0F88) "(3) Invalid request URI or method\n") ;; 34 (data (i32.const 0x0FB0) "Redirecting to ") ;; 15 (data (i32.const 0x0FC0) "Cross-origin redirect; stripping Authorization/Cookie\n") ;; 54 (data (i32.const 0x0FF8) "Connecting to ") ;; 14 (data (i32.const 0x1008) "Header rejected by host as forbidden: ") ;; 38 (data (i32.const 0x1030) "Invalid header syntax: ") ;; 23 (data (i32.const 0x1048) "Invalid numeric value for option ") ;; 33 (data (i32.const 0x1070) "Too many headers (max 128)\n") ;; 27 (data (i32.const 0x1090) "Too many URLs (max 64)\n") ;; 23 (data (i32.const 0x10A8) "Stream I/O error\n") ;; 17 (data (i32.const 0x10C0) "Upload complete: ") ;; 17 (data (i32.const 0x10D8) " bytes\n") ;; 7 (data (i32.const 0x10E0) "(1) HTTP error: ") ;; 15 ;; HTTP Status Reason Phrases (0x1200 .. 0x16FF) (data (i32.const 0x1200) "Continue") ;; 8 (data (i32.const 0x1210) "Switching Protocols") ;; 19 (data (i32.const 0x1228) "OK") ;; 2 (data (i32.const 0x1230) "Created") ;; 7 (data (i32.const 0x1238) "Accepted") ;; 8 (data (i32.const 0x1248) "No Content") ;; 10 (data (i32.const 0x1258) "Partial Content") ;; 15 (data (i32.const 0x1268) "Multiple Choices") ;; 16 (data (i32.const 0x1280) "Moved Permanently") ;; 17 (data (i32.const 0x1298) "Found") ;; 5 (data (i32.const 0x12A0) "See Other") ;; 9 (data (i32.const 0x12B0) "Not Modified") ;; 12 (data (i32.const 0x12C0) "Temporary Redirect") ;; 18 (data (i32.const 0x12D8) "Permanent Redirect") ;; 18 (data (i32.const 0x12F0) "Bad Request") ;; 11 (data (i32.const 0x1300) "Unauthorized") ;; 12 (data (i32.const 0x1310) "Forbidden") ;; 9 (data (i32.const 0x1320) "Not Found") ;; 9 (data (i32.const 0x1330) "Method Not Allowed") ;; 18 (data (i32.const 0x1348) "Request Timeout") ;; 15 (data (i32.const 0x1358) "Conflict") ;; 8 (data (i32.const 0x1368) "Gone") ;; 4 (data (i32.const 0x1370) "Length Required") ;; 15 (data (i32.const 0x1380) "Payload Too Large") ;; 17 (data (i32.const 0x1398) "URI Too Long") ;; 12 (data (i32.const 0x13A8) "Unsupported Media Type") ;; 22 (data (i32.const 0x13C0) "Range Not Satisfiable") ;; 21 (data (i32.const 0x13D8) "I'm a teapot") ;; 12 (data (i32.const 0x13E8) "Unprocessable Content") ;; 21 (data (i32.const 0x1400) "Too Many Requests") ;; 17 (data (i32.const 0x1418) "Internal Server Error") ;; 21 (data (i32.const 0x1430) "Not Implemented") ;; 15 (data (i32.const 0x1440) "Bad Gateway") ;; 11 (data (i32.const 0x1450) "Service Unavailable") ;; 19 (data (i32.const 0x1468) "Gateway Timeout") ;; 15 ;; Long CLI option names (0x1600 .. 0x1FFF) (data (i32.const 0x1600) "--help") ;; 6 (data (i32.const 0x1608) "--version") ;; 9 (data (i32.const 0x1614) "--request") ;; 9 (data (i32.const 0x1620) "--header") ;; 8 (data (i32.const 0x162C) "--data") ;; 6 (data (i32.const 0x1634) "--data-ascii") ;; 12 (data (i32.const 0x1644) "--data-raw") ;; 10 (data (i32.const 0x1650) "--data-binary") ;; 13 (data (i32.const 0x1660) "--data-urlencode") ;; 16 (data (i32.const 0x1674) "--get") ;; 5 (data (i32.const 0x167C) "--upload-file") ;; 13 (data (i32.const 0x168C) "--include") ;; 9 (data (i32.const 0x1698) "--head") ;; 6 (data (i32.const 0x16A0) "--location") ;; 10 (data (i32.const 0x16AC) "--max-redirs") ;; 12 (data (i32.const 0x16BC) "--output") ;; 8 (data (i32.const 0x16C8) "--remote-name") ;; 13 (data (i32.const 0x16D8) "--dump-header") ;; 13 (data (i32.const 0x16E8) "--silent") ;; 8 (data (i32.const 0x16F4) "--show-error") ;; 12 (data (i32.const 0x1704) "--verbose") ;; 9 (data (i32.const 0x1710) "--fail") ;; 6 (data (i32.const 0x1718) "--user") ;; 6 (data (i32.const 0x1720) "--user-agent") ;; 12 (data (i32.const 0x1730) "--referer") ;; 9 (data (i32.const 0x173C) "--cookie") ;; 8 (data (i32.const 0x1748) "--range") ;; 7 (data (i32.const 0x1754) "--write-out") ;; 11 (data (i32.const 0x1764) "--connect-timeout") ;; 17 (data (i32.const 0x1778) "--max-time") ;; 10 ;; --write-out variable names (0x1800 .. 0x18FF) (data (i32.const 0x1800) "http_code") ;; 9 (data (i32.const 0x180C) "response_code") ;; 13 (data (i32.const 0x181C) "size_download") ;; 13 (data (i32.const 0x182C) "size_upload") ;; 11 (data (i32.const 0x1838) "time_total") ;; 10 (data (i32.const 0x1844) "url_effective") ;; 13 (data (i32.const 0x1854) "content_type") ;; 12 (data (i32.const 0x1864) "redirect_url") ;; 12 (data (i32.const 0x1874) "num_redirects") ;; 13 (data (i32.const 0x1884) "scheme") ;; 6 (data (i32.const 0x188C) "method") ;; 6 ;; WASI HTTP Error Names 0..38 (0x2000 .. 0x28FF) (data (i32.const 0x2000) "DNS timeout") ;; 0 len 11 (data (i32.const 0x2010) "DNS error") ;; 1 len 9 (data (i32.const 0x2020) "destination not found") ;; 2 len 21 (data (i32.const 0x2038) "destination unavailable") ;; 3 len 23 (data (i32.const 0x2050) "destination IP prohibited") ;; 4 len 25 (data (i32.const 0x2070) "destination IP unroutable") ;; 5 len 25 (data (i32.const 0x2090) "connection refused") ;; 6 len 18 (data (i32.const 0x20A8) "connection terminated") ;; 7 len 21 (data (i32.const 0x20C0) "connection timeout") ;; 8 len 18 (data (i32.const 0x20D8) "connection read timeout") ;; 9 len 23 (data (i32.const 0x20F0) "connection write timeout") ;; 10 len 24 (data (i32.const 0x2110) "connection limit reached") ;; 11 len 24 (data (i32.const 0x2130) "TLS protocol error") ;; 12 len 18 (data (i32.const 0x2148) "TLS certificate error") ;; 13 len 21 (data (i32.const 0x2160) "TLS alert received") ;; 14 len 18 (data (i32.const 0x2178) "HTTP request denied") ;; 15 len 19 (data (i32.const 0x2190) "HTTP request length required") ;; 16 len 28 (data (i32.const 0x21B0) "HTTP request body size error") ;; 17 len 28 (data (i32.const 0x21D0) "HTTP request method invalid") ;; 18 len 27 (data (i32.const 0x21F0) "HTTP request URI invalid") ;; 19 len 24 (data (i32.const 0x2210) "HTTP request URI too long") ;; 20 len 25 (data (i32.const 0x2230) "HTTP request header section too large") ;; 21 len 37 (data (i32.const 0x2258) "HTTP request header too large") ;; 22 len 29 (data (i32.const 0x2278) "HTTP request trailer section too large") ;; 23 len 38 (data (i32.const 0x22A0) "HTTP request trailer too large") ;; 24 len 30 (data (i32.const 0x22C0) "HTTP response incomplete") ;; 25 len 24 (data (i32.const 0x22E0) "HTTP response header section too large") ;; 26 len 38 (data (i32.const 0x2308) "HTTP response header too large") ;; 27 len 30 (data (i32.const 0x2328) "HTTP response body size error") ;; 28 len 29 (data (i32.const 0x2348) "HTTP response trailer section too large") ;; 29 len 39 (data (i32.const 0x2370) "HTTP response trailer too large") ;; 30 len 31 (data (i32.const 0x2390) "HTTP response transfer coding error") ;; 31 len 35 (data (i32.const 0x23B8) "HTTP response content coding error") ;; 32 len 34 (data (i32.const 0x23E0) "HTTP response timeout") ;; 33 len 21 (data (i32.const 0x23F8) "HTTP upgrade failed") ;; 34 len 19 (data (i32.const 0x2410) "HTTP protocol error") ;; 35 len 19 (data (i32.const 0x2428) "redirect loop detected") ;; 36 len 22 (data (i32.const 0x2440) "configuration error") ;; 37 len 19 (data (i32.const 0x2458) "internal error") ;; 38 len 14 ;; ========================================================================== ;; Scratch Addresses & Tables (0x7000 .. 0xFFFF) ;; ========================================================================== ;; 0x7000: RETPTR_IO (64B) ;; 0x7040: RETPTR_HTTP (64B) ;; 0x7080: RETPTR_FS (64B) ;; 0x70C0: RETPTR_CLI (64B) ;; 0x7100: RETPTR_TMP (64B) ;; 0x7400: CLI_HDRS (128 * 20 = 2560B -> 0x7E00) ;; 0x8000: CLI_URLS (64 * 8 = 512B -> 0x8200) ;; 0x8400: BUF_FMT (3072B -> 0x9000) ;; 0x9000: BUF_URL (12288B -> 0xC000) ;; 0xC000: BUF_B64 (8192B -> 0xE000) ;; 0xE000: BUF_SCRATCH (8192B -> 0x10000) ;; ========================================================================== ;; Global State ;; ========================================================================== (global $g_stdin (mut i32) (i32.const 0)) (global $g_stdout (mut i32) (i32.const 0)) (global $g_stderr (mut i32) (i32.const 0)) (global $g_preopens_init (mut i32) (i32.const 0)) (global $g_preopens_ptr (mut i32) (i32.const 0)) (global $g_preopens_cnt (mut i32) (i32.const 0)) ;; CLI Option Flags & Values (global $opt_help (mut i32) (i32.const 0)) (global $opt_version (mut i32) (i32.const 0)) (global $opt_method_ptr (mut i32) (i32.const 0)) (global $opt_method_len (mut i32) (i32.const 0)) (global $opt_get (mut i32) (i32.const 0)) (global $opt_include (mut i32) (i32.const 0)) (global $opt_head (mut i32) (i32.const 0)) (global $opt_location (mut i32) (i32.const 0)) (global $opt_max_redirs (mut i32) (i32.const 50)) (global $opt_output_set (mut i32) (i32.const 0)) (global $opt_output_ptr (mut i32) (i32.const 0)) (global $opt_output_len (mut i32) (i32.const 0)) (global $opt_remote_name (mut i32) (i32.const 0)) (global $opt_dump_hdr_set (mut i32) (i32.const 0)) (global $opt_dump_hdr_ptr (mut i32) (i32.const 0)) (global $opt_dump_hdr_len (mut i32) (i32.const 0)) (global $opt_silent (mut i32) (i32.const 0)) (global $opt_show_error (mut i32) (i32.const 0)) (global $opt_verbose (mut i32) (i32.const 0)) (global $opt_fail (mut i32) (i32.const 0)) (global $opt_user_set (mut i32) (i32.const 0)) (global $opt_user_ptr (mut i32) (i32.const 0)) (global $opt_user_len (mut i32) (i32.const 0)) (global $opt_ua_set (mut i32) (i32.const 0)) (global $opt_ua_ptr (mut i32) (i32.const 0)) (global $opt_ua_len (mut i32) (i32.const 0)) (global $opt_referer_set (mut i32) (i32.const 0)) (global $opt_referer_ptr (mut i32) (i32.const 0)) (global $opt_referer_len (mut i32) (i32.const 0)) (global $opt_cookie_set (mut i32) (i32.const 0)) (global $opt_cookie_ptr (mut i32) (i32.const 0)) (global $opt_cookie_len (mut i32) (i32.const 0)) (global $opt_range_set (mut i32) (i32.const 0)) (global $opt_range_ptr (mut i32) (i32.const 0)) (global $opt_range_len (mut i32) (i32.const 0)) (global $opt_write_out_set (mut i32) (i32.const 0)) (global $opt_write_out_ptr (mut i32) (i32.const 0)) (global $opt_write_out_len (mut i32) (i32.const 0)) (global $opt_upload_set (mut i32) (i32.const 0)) (global $opt_upload_ptr (mut i32) (i32.const 0)) (global $opt_upload_len (mut i32) (i32.const 0)) (global $opt_connect_timeout_set (mut i32) (i32.const 0)) (global $opt_connect_timeout_ns (mut i64) (i64.const 0)) (global $opt_max_time_set (mut i32) (i32.const 0)) (global $opt_max_time_ns (mut i64) (i64.const 0)) (global $cli_hdr_cnt (mut i32) (i32.const 0)) (global $cli_url_cnt (mut i32) (i32.const 0)) ;; Accumulated -d / --data* buffer (global $has_data (mut i32) (i32.const 0)) (global $data_buf_ptr (mut i32) (i32.const 0)) (global $data_buf_len (mut i32) (i32.const 0)) (global $data_buf_cap (mut i32) (i32.const 0)) ;; Multi-return temp globals (for 2-value internal returns) (global $ret_ptr (mut i32) (i32.const 0)) (global $ret_len (mut i32) (i32.const 0)) (global $ret_val3 (mut i32) (i32.const 0)) ;; Parsed current URL state (global $url_scheme (mut i32) (i32.const 0)) ;; 0=HTTP, 1=HTTPS (global $url_auth_ptr (mut i32) (i32.const 0)) (global $url_auth_len (mut i32) (i32.const 0)) (global $url_path_ptr (mut i32) (i32.const 0)) (global $url_path_len (mut i32) (i32.const 0)) (global $url_has_userinfo (mut i32) (i32.const 0)) (global $url_userinfo_ptr (mut i32) (i32.const 0)) (global $url_userinfo_len (mut i32) (i32.const 0)) (global $url_eff_ptr (mut i32) (i32.const 0)) (global $url_eff_len (mut i32) (i32.const 0)) ;; Active request/transfer state (global $req_method_ptr (mut i32) (i32.const 0)) (global $req_method_len (mut i32) (i32.const 0)) (global $req_has_body (mut i32) (i32.const 0)) (global $req_body_ptr (mut i32) (i32.const 0)) (global $req_body_len (mut i32) (i32.const 0)) (global $req_is_form (mut i32) (i32.const 0)) (global $cross_origin_redir (mut i32) (i32.const 0)) ;; Output sinks for current transfer ;; out_mode: 0=stdout, 1=file, 2=/dev/null (global $cur_out_mode (mut i32) (i32.const 0)) (global $cur_out_fd (mut i32) (i32.const 0)) (global $cur_out_stream (mut i32) (i32.const 0)) (global $cur_out_opened (mut i32) (i32.const 0)) (global $eff_out_path_ptr (mut i32) (i32.const 0)) (global $eff_out_path_len (mut i32) (i32.const 0)) (global $eff_out_is_file (mut i32) (i32.const 0)) ;; dump-header sink: 0=none, 1=stdout, 2=file, 3=/dev/null (global $cur_dump_mode (mut i32) (i32.const 0)) (global $cur_dump_fd (mut i32) (i32.const 0)) (global $cur_dump_stream (mut i32) (i32.const 0)) (global $cur_dump_opened (mut i32) (i32.const 0)) ;; Hop location / stats for -w (global $hop_loc_ptr (mut i32) (i32.const 0)) (global $hop_loc_len (mut i32) (i32.const 0)) (global $stat_status (mut i32) (i32.const 0)) (global $stat_size_down (mut i64) (i64.const 0)) (global $stat_size_up (mut i64) (i64.const 0)) (global $stat_start_ns (mut i64) (i64.const 0)) (global $stat_elapsed_ns (mut i64) (i64.const 0)) (global $stat_deadline_ns (mut i64) (i64.const 0)) (global $stat_num_redirs (mut i32) (i32.const 0)) (global $stat_ct_ptr (mut i32) (i32.const 0)) (global $stat_ct_len (mut i32) (i32.const 0)) (global $stat_redir_ptr (mut i32) (i32.const 0)) (global $stat_redir_len (mut i32) (i32.const 0)) ;; ========================================================================== ;; 4a. Byte / String / Number / Encoding Utilities ;; ========================================================================== (func $min_u (param $a i32) (param $b i32) (result i32) (select (local.get $a) (local.get $b) (i32.lt_u (local.get $a) (local.get $b))) ) (func $ascii_lower (param $c i32) (result i32) (if (i32.and (i32.ge_u (local.get $c) (i32.const 65)) (i32.le_u (local.get $c) (i32.const 90))) (then (return (i32.add (local.get $c) (i32.const 32)))) ) (local.get $c) ) (func $memeq (param $a i32) (param $alen i32) (param $b i32) (param $blen i32) (result i32) (local $i i32) (if (i32.ne (local.get $alen) (local.get $blen)) (then (return (i32.const 0)))) (loop $l (if (i32.lt_u (local.get $i) (local.get $alen)) (then (if (i32.ne (i32.load8_u (i32.add (local.get $a) (local.get $i))) (i32.load8_u (i32.add (local.get $b) (local.get $i)))) (then (return (i32.const 0))) ) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l) )) ) (i32.const 1) ) (func $memeq_ci (param $a i32) (param $alen i32) (param $b i32) (param $blen i32) (result i32) (local $i i32) (if (i32.ne (local.get $alen) (local.get $blen)) (then (return (i32.const 0)))) (loop $l (if (i32.lt_u (local.get $i) (local.get $alen)) (then (if (i32.ne (call $ascii_lower (i32.load8_u (i32.add (local.get $a) (local.get $i)))) (call $ascii_lower (i32.load8_u (i32.add (local.get $b) (local.get $i))))) (then (return (i32.const 0))) ) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l) )) ) (i32.const 1) ) (func $starts_with (param $p i32) (param $len i32) (param $pfx i32) (param $pfx_len i32) (result i32) (if (i32.lt_u (local.get $len) (local.get $pfx_len)) (then (return (i32.const 0)))) (call $memeq (local.get $p) (local.get $pfx_len) (local.get $pfx) (local.get $pfx_len)) ) (func $find_byte (param $p i32) (param $len i32) (param $ch i32) (result i32) (local $i i32) (loop $l (if (i32.lt_u (local.get $i) (local.get $len)) (then (if (i32.eq (i32.load8_u (i32.add (local.get $p) (local.get $i))) (local.get $ch)) (then (return (local.get $i))) ) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l) )) ) (i32.const -1) ) (func $rfind_byte (param $p i32) (param $len i32) (param $ch i32) (result i32) (local $i i32) (local.set $i (local.get $len)) (loop $l (if (i32.gt_u (local.get $i) (i32.const 0)) (then (local.set $i (i32.sub (local.get $i) (i32.const 1))) (if (i32.eq (i32.load8_u (i32.add (local.get $p) (local.get $i))) (local.get $ch)) (then (return (local.get $i))) ) (br $l) )) ) (i32.const -1) ) (func $find_bytes2 (param $p i32) (param $len i32) (param $c1 i32) (param $c2 i32) (result i32) (local $i i32) (local $b i32) (loop $l (if (i32.lt_u (local.get $i) (local.get $len)) (then (local.set $b (i32.load8_u (i32.add (local.get $p) (local.get $i)))) (if (i32.or (i32.eq (local.get $b) (local.get $c1)) (i32.eq (local.get $b) (local.get $c2))) (then (return (local.get $i))) ) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l) )) ) (i32.const -1) ) ;; Find "://" in p[0..len], returns index or -1 (func $find_scheme_sep (param $p i32) (param $len i32) (result i32) (local $i i32) (if (i32.lt_u (local.get $len) (i32.const 3)) (then (return (i32.const -1)))) (loop $l (if (i32.le_u (i32.add (local.get $i) (i32.const 3)) (local.get $len)) (then (if (i32.and (i32.eq (i32.load8_u (i32.add (local.get $p) (local.get $i))) (i32.const 58)) ;; ':' (i32.and (i32.eq (i32.load8_u (i32.add (local.get $p) (i32.add (local.get $i) (i32.const 1)))) (i32.const 47)) (i32.eq (i32.load8_u (i32.add (local.get $p) (i32.add (local.get $i) (i32.const 2)))) (i32.const 47)))) (then (return (local.get $i))) ) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l) )) ) (i32.const -1) ) (func $heap_strdup (param $p i32) (param $len i32) (result i32) (local $dst i32) (local.set $dst (call $alloc (local.get $len) (i32.const 1))) (if (i32.gt_u (local.get $len) (i32.const 0)) (then (memory.copy (local.get $dst) (local.get $p) (local.get $len))) ) (local.get $dst) ) ;; Trim OWS (' ', '\t', '\r', '\n') -> sets $ret_ptr, $ret_len (func $trim_ws (param $p i32) (param $len i32) (local $s i32) (local $e i32) (local $c i32) (local.set $s (i32.const 0)) (local.set $e (local.get $len)) (loop $l1 (if (i32.lt_u (local.get $s) (local.get $e)) (then (local.set $c (i32.load8_u (i32.add (local.get $p) (local.get $s)))) (if (i32.or (i32.or (i32.eq (local.get $c) (i32.const 32)) (i32.eq (local.get $c) (i32.const 9))) (i32.or (i32.eq (local.get $c) (i32.const 13)) (i32.eq (local.get $c) (i32.const 10)))) (then (local.set $s (i32.add (local.get $s) (i32.const 1))) (br $l1) ) ) )) ) (loop $l2 (if (i32.gt_u (local.get $e) (local.get $s)) (then (local.set $c (i32.load8_u (i32.add (local.get $p) (i32.sub (local.get $e) (i32.const 1))))) (if (i32.or (i32.or (i32.eq (local.get $c) (i32.const 32)) (i32.eq (local.get $c) (i32.const 9))) (i32.or (i32.eq (local.get $c) (i32.const 13)) (i32.eq (local.get $c) (i32.const 10)))) (then (local.set $e (i32.sub (local.get $e) (i32.const 1))) (br $l2) ) ) )) ) (global.set $ret_ptr (i32.add (local.get $p) (local.get $s))) (global.set $ret_len (i32.sub (local.get $e) (local.get $s))) ) ;; Format u64 into buf (at least 24 bytes), returns length (func $u64_to_dec (param $val i64) (param $buf i32) (result i32) (local $n i64) (local $i i32) (local $j i32) (local $tmp i32) (local.set $n (local.get $val)) (if (i64.eq (local.get $n) (i64.const 0)) (then (i32.store8 (local.get $buf) (i32.const 48)) (return (i32.const 1)) )) (loop $l (if (i64.gt_u (local.get $n) (i64.const 0)) (then (i32.store8 (i32.add (local.get $buf) (local.get $i)) (i32.add (i32.wrap_i64 (i64.rem_u (local.get $n) (i64.const 10))) (i32.const 48)) ) (local.set $n (i64.div_u (local.get $n) (i64.const 10))) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l) )) ) ;; reverse (local.set $j (i32.const 0)) (loop $rev (if (i32.lt_u (local.get $j) (i32.div_u (local.get $i) (i32.const 2))) (then (local.set $tmp (i32.load8_u (i32.add (local.get $buf) (local.get $j)))) (i32.store8 (i32.add (local.get $buf) (local.get $j)) (i32.load8_u (i32.add (local.get $buf) (i32.sub (i32.sub (local.get $i) (i32.const 1)) (local.get $j)))) ) (i32.store8 (i32.add (local.get $buf) (i32.sub (i32.sub (local.get $i) (i32.const 1)) (local.get $j))) (local.get $tmp) ) (local.set $j (i32.add (local.get $j) (i32.const 1))) (br $rev) )) ) (local.get $i) ) ;; Format u32 with minimum zero-padded width (e.g. width=3 -> "200" or "000") (func $u32_to_dec_pad (param $val i32) (param $buf i32) (param $min_w i32) (result i32) (local $len i32) (local $pad i32) (local.set $len (call $u64_to_dec (i64.extend_i32_u (local.get $val)) (i32.add (local.get $buf) (i32.const 32)))) (if (i32.lt_u (local.get $len) (local.get $min_w)) (then (local.set $pad (i32.sub (local.get $min_w) (local.get $len)))) ) (if (i32.gt_u (local.get $pad) (i32.const 0)) (then (memory.fill (local.get $buf) (i32.const 48) (local.get $pad)) )) (memory.copy (i32.add (local.get $buf) (local.get $pad)) (i32.add (local.get $buf) (i32.const 32)) (local.get $len)) (i32.add (local.get $pad) (local.get $len)) ) ;; Format nanoseconds -> S.FFFFFF into buf, returns length (func $format_time_total (param $ns i64) (param $buf i32) (result i32) (local $secs i64) (local $usec i32) (local $slen i32) (local.set $secs (i64.div_u (local.get $ns) (i64.const 1000000000))) (local.set $usec (i32.wrap_i64 (i64.div_u (i64.rem_u (local.get $ns) (i64.const 1000000000)) (i64.const 1000)))) (local.set $slen (call $u64_to_dec (local.get $secs) (local.get $buf))) (i32.store8 (i32.add (local.get $buf) (local.get $slen)) (i32.const 46)) ;; '.' (drop (call $u32_to_dec_pad (local.get $usec) (i32.add (local.get $buf) (i32.add (local.get $slen) (i32.const 1))) (i32.const 6))) (i32.add (local.get $slen) (i32.const 7)) ) ;; Parse signed integer; returns 0 ok (value in $ret_ptr), 1 err (func $parse_i32 (param $p i32) (param $len i32) (result i32) (local $i i32) (local $neg i32) (local $acc i64) (local $c i32) (if (i32.eq (local.get $len) (i32.const 0)) (then (return (i32.const 1)))) (if (i32.eq (i32.load8_u (local.get $p)) (i32.const 45)) (then (local.set $neg (i32.const 1)) (local.set $i (i32.const 1)) )) (if (i32.ge_u (local.get $i) (local.get $len)) (then (return (i32.const 1)))) (loop $l (if (i32.lt_u (local.get $i) (local.get $len)) (then (local.set $c (i32.load8_u (i32.add (local.get $p) (local.get $i)))) (if (i32.or (i32.lt_u (local.get $c) (i32.const 48)) (i32.gt_u (local.get $c) (i32.const 57))) (then (return (i32.const 1))) ) (local.set $acc (i64.add (i64.mul (local.get $acc) (i64.const 10)) (i64.extend_i32_u (i32.sub (local.get $c) (i32.const 48))))) (if (i64.gt_u (local.get $acc) (i64.const 2147483647)) (then (return (i32.const 1)))) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l) )) ) (if (local.get $neg) (then (global.set $ret_ptr (i32.sub (i32.const 0) (i32.wrap_i64 (local.get $acc))))) (else (global.set $ret_ptr (i32.wrap_i64 (local.get $acc)))) ) (i32.const 0) ) ;; Parse decimal seconds (e.g. "0.25", "5") to nanoseconds -> $ret_val3 ignored, returns i64 via $stat_elapsed_ns? Let's use a local-safe helper returning 0/1 and storing ns in $ret_ptr/$ret_len? Wait: use a dedicated global $tmp_u64! (global $tmp_u64 (mut i64) (i64.const 0)) (func $parse_seconds_to_ns (param $p i32) (param $len i32) (result i32) (local $i i32) (local $c i32) (local $whole i64) (local $frac i64) (local $scale i64) (local $seen_dot i32) (local $seen_digit i32) (if (i32.eq (local.get $len) (i32.const 0)) (then (return (i32.const 1)))) (local.set $scale (i64.const 100000000)) ;; 1e8 (loop $l (if (i32.lt_u (local.get $i) (local.get $len)) (then (local.set $c (i32.load8_u (i32.add (local.get $p) (local.get $i)))) (if (i32.eq (local.get $c) (i32.const 46)) (then (if (local.get $seen_dot) (then (return (i32.const 1)))) (local.set $seen_dot (i32.const 1)) )(else (if (i32.or (i32.lt_u (local.get $c) (i32.const 48)) (i32.gt_u (local.get $c) (i32.const 57))) (then (return (i32.const 1))) ) (local.set $seen_digit (i32.const 1)) (if (local.get $seen_dot) (then (if (i64.gt_u (local.get $scale) (i64.const 0)) (then (local.set $frac (i64.add (local.get $frac) (i64.mul (i64.extend_i32_u (i32.sub (local.get $c) (i32.const 48))) (local.get $scale)))) (local.set $scale (i64.div_u (local.get $scale) (i64.const 10))) )) ) (else (local.set $whole (i64.add (i64.mul (local.get $whole) (i64.const 10)) (i64.extend_i32_u (i32.sub (local.get $c) (i32.const 48))))) (if (i64.gt_u (local.get $whole) (i64.const 100000000)) (then (return (i32.const 1)))) ) ) )) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l) )) ) (if (i32.eqz (local.get $seen_digit)) (then (return (i32.const 1)))) (global.set $tmp_u64 (i64.add (i64.mul (local.get $whole) (i64.const 1000000000)) (local.get $frac))) (i32.const 0) ) ;; Base64 encode src[0..slen] into dst; returns encoded length (func $base64_encode (param $src i32) (param $slen i32) (param $dst i32) (result i32) (local $i i32) (local $o i32) (local $b0 i32) (local $b1 i32) (local $b2 i32) (local $t i32) (loop $l (if (i32.lt_u (local.get $i) (local.get $slen)) (then (local.set $b0 (i32.load8_u (i32.add (local.get $src) (local.get $i)))) (local.set $b1 (select (i32.load8_u (i32.add (local.get $src) (i32.add (local.get $i) (i32.const 1)))) (i32.const 0) (i32.lt_u (i32.add (local.get $i) (i32.const 1)) (local.get $slen)) )) (local.set $b2 (select (i32.load8_u (i32.add (local.get $src) (i32.add (local.get $i) (i32.const 2)))) (i32.const 0) (i32.lt_u (i32.add (local.get $i) (i32.const 2)) (local.get $slen)) )) (local.set $t (i32.or (i32.or (i32.shl (local.get $b0) (i32.const 16)) (i32.shl (local.get $b1) (i32.const 8))) (local.get $b2))) (i32.store8 (i32.add (local.get $dst) (local.get $o)) (i32.load8_u (i32.add (i32.const 0x0C00) (i32.and (i32.shr_u (local.get $t) (i32.const 18)) (i32.const 63))))) (i32.store8 (i32.add (local.get $dst) (i32.add (local.get $o) (i32.const 1))) (i32.load8_u (i32.add (i32.const 0x0C00) (i32.and (i32.shr_u (local.get $t) (i32.const 12)) (i32.const 63))))) (if (i32.lt_u (i32.add (local.get $i) (i32.const 1)) (local.get $slen)) (then (i32.store8 (i32.add (local.get $dst) (i32.add (local.get $o) (i32.const 2))) (i32.load8_u (i32.add (i32.const 0x0C00) (i32.and (i32.shr_u (local.get $t) (i32.const 6)) (i32.const 63))))) ) (else (i32.store8 (i32.add (local.get $dst) (i32.add (local.get $o) (i32.const 2))) (i32.const 61)) ;; '=' ) ) (if (i32.lt_u (i32.add (local.get $i) (i32.const 2)) (local.get $slen)) (then (i32.store8 (i32.add (local.get $dst) (i32.add (local.get $o) (i32.const 3))) (i32.load8_u (i32.add (i32.const 0x0C00) (i32.and (local.get $t) (i32.const 63))))) ) (else (i32.store8 (i32.add (local.get $dst) (i32.add (local.get $o) (i32.const 3))) (i32.const 61)) ) ) (local.set $i (i32.add (local.get $i) (i32.const 3))) (local.set $o (i32.add (local.get $o) (i32.const 4))) (br $l) )) ) (local.get $o) ) ;; Growable $data_buf append helpers (func $data_buf_ensure (param $extra i32) (local $need i32) (local $new_cap i32) (local.set $need (i32.add (global.get $data_buf_len) (local.get $extra))) (if (i32.le_u (local.get $need) (global.get $data_buf_cap)) (then (return))) (local.set $new_cap (select (i32.const 256) (global.get $data_buf_cap) (i32.eq (global.get $data_buf_cap) (i32.const 0)))) (loop $grow (if (i32.lt_u (local.get $new_cap) (local.get $need)) (then (local.set $new_cap (i32.mul (local.get $new_cap) (i32.const 2))) (br $grow) )) ) (global.set $data_buf_ptr (call $realloc (global.get $data_buf_ptr) (global.get $data_buf_len) (i32.const 1) (local.get $new_cap)) ) (global.set $data_buf_cap (local.get $new_cap)) ) (func $data_buf_append_bytes (param $p i32) (param $len i32) (if (i32.eq (local.get $len) (i32.const 0)) (then (return))) (call $data_buf_ensure (local.get $len)) (memory.copy (i32.add (global.get $data_buf_ptr) (global.get $data_buf_len)) (local.get $p) (local.get $len)) (global.set $data_buf_len (i32.add (global.get $data_buf_len) (local.get $len))) ) (func $data_buf_append_byte (param $b i32) (call $data_buf_ensure (i32.const 1)) (i32.store8 (i32.add (global.get $data_buf_ptr) (global.get $data_buf_len)) (local.get $b)) (global.set $data_buf_len (i32.add (global.get $data_buf_len) (i32.const 1))) ) (func $is_unreserved (param $c i32) (result i32) (if (i32.and (i32.ge_u (local.get $c) (i32.const 48)) (i32.le_u (local.get $c) (i32.const 57))) (then (return (i32.const 1)))) (if (i32.and (i32.ge_u (local.get $c) (i32.const 65)) (i32.le_u (local.get $c) (i32.const 90))) (then (return (i32.const 1)))) (if (i32.and (i32.ge_u (local.get $c) (i32.const 97)) (i32.le_u (local.get $c) (i32.const 122))) (then (return (i32.const 1)))) (if (i32.or (i32.or (i32.eq (local.get $c) (i32.const 45)) (i32.eq (local.get $c) (i32.const 46))) (i32.or (i32.eq (local.get $c) (i32.const 95)) (i32.eq (local.get $c) (i32.const 126)))) (then (return (i32.const 1))) ) (i32.const 0) ) (func $data_buf_append_urlencoded (param $p i32) (param $len i32) (local $i i32) (local $c i32) (loop $l (if (i32.lt_u (local.get $i) (local.get $len)) (then (local.set $c (i32.load8_u (i32.add (local.get $p) (local.get $i)))) (if (call $is_unreserved (local.get $c)) (then (call $data_buf_append_byte (local.get $c))) (else (call $data_buf_append_byte (i32.const 37)) ;; '%' (call $data_buf_append_byte (i32.load8_u (i32.add (i32.const 0x0C50) (i32.shr_u (local.get $c) (i32.const 4))))) (call $data_buf_append_byte (i32.load8_u (i32.add (i32.const 0x0C50) (i32.and (local.get $c) (i32.const 15))))) ) ) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l) )) ) ) ;; ========================================================================== ;; 4b. WASI Stream & Filesystem Helpers ;; ========================================================================== ;; Write all bytes of ptr[0..len] via output-stream using check-write + write + subscribe/block ;; Returns 0 ok, 1 error (func $stream_write_all (param $stream i32) (param $ptr i32) (param $len i32) (result i32) (local $off i32) (local $avail i64) (local $chunk i32) (local $tag i32) (local $se i32) (local $p i32) (loop $l (if (i32.lt_u (local.get $off) (local.get $len)) (then (call $out_check_write (local.get $stream) (i32.const 0x7000)) (local.set $tag (i32.load8_u (i32.const 0x7000))) (if (i32.ne (local.get $tag) (i32.const 0)) (then (local.set $se (i32.load8_u (i32.const 0x7008))) (if (i32.eq (local.get $se) (i32.const 0)) (then (call $drop_io_error (i32.load (i32.const 0x700C))) )) (return (i32.const 1)) )) (local.set $avail (i64.load (i32.const 0x7008))) (if (i64.eq (local.get $avail) (i64.const 0)) (then (local.set $p (call $out_subscribe (local.get $stream))) (call $poll_block (local.get $p)) (call $drop_pollable (local.get $p)) (br $l) )) (local.set $chunk (i32.sub (local.get $len) (local.get $off))) (if (i64.lt_u (local.get $avail) (i64.extend_i32_u (local.get $chunk))) (then (local.set $chunk (i32.wrap_i64 (local.get $avail))) )) (if (i32.gt_u (local.get $chunk) (i32.const 65536)) (then (local.set $chunk (i32.const 65536)) )) (call $out_write (local.get $stream) (i32.add (local.get $ptr) (local.get $off)) (local.get $chunk) (i32.const 0x7000)) (if (i32.ne (i32.load8_u (i32.const 0x7000)) (i32.const 0)) (then (if (i32.eq (i32.load8_u (i32.const 0x7004)) (i32.const 0)) (then (call $drop_io_error (i32.load (i32.const 0x7008))) )) (return (i32.const 1)) )) (local.set $off (i32.add (local.get $off) (local.get $chunk))) (br $l) )) ) (i32.const 0) ) (func $stream_flush (param $stream i32) (result i32) (call $out_blocking_flush (local.get $stream) (i32.const 0x7000)) (if (i32.ne (i32.load8_u (i32.const 0x7000)) (i32.const 0)) (then (if (i32.eq (i32.load8_u (i32.const 0x7004)) (i32.const 0)) (then (call $drop_io_error (i32.load (i32.const 0x7008))) )) (return (i32.const 1)) )) (i32.const 0) ) (func $write_out (param $p i32) (param $len i32) (if (i32.gt_u (local.get $len) (i32.const 0)) (then (drop (call $stream_write_all (global.get $g_stdout) (local.get $p) (local.get $len))) )) ) (func $write_err (param $p i32) (param $len i32) (if (i32.gt_u (local.get $len) (i32.const 0)) (then (drop (call $stream_write_all (global.get $g_stderr) (local.get $p) (local.get $len))) )) ) ;; Should runtime error be printed? (!silent || show_error) (func $should_show_err (result i32) (i32.or (i32.eqz (global.get $opt_silent)) (global.get $opt_show_error)) ) (func $write_u64_to (param $stream i32) (param $v i64) (local $n i32) (local.set $n (call $u64_to_dec (local.get $v) (i32.const 0x8400))) (drop (call $stream_write_all (local.get $stream) (i32.const 0x8400) (local.get $n))) ) (func $write_u32_to (param $stream i32) (param $v i32) (call $write_u64_to (local.get $stream) (i64.extend_i32_u (local.get $v))) ) ;; Read entire input-stream into heap buffer; sets $ret_ptr, $ret_len. Returns 0 ok, 1 err (func $read_stream_all (param $in_stream i32) (param $strip_crlf i32) (result i32) (local $buf i32) (local $cap i32) (local $len i32) (local $cptr i32) (local $clen i32) (local $i i32) (local $b i32) (local $ncap i32) (local.set $cap (i32.const 4096)) (local.set $buf (call $alloc (local.get $cap) (i32.const 1))) (loop $l (call $in_blocking_read (local.get $in_stream) (i64.const 65536) (i32.const 0x7000)) (if (i32.eq (i32.load8_u (i32.const 0x7000)) (i32.const 0)) (then (local.set $cptr (i32.load (i32.const 0x7004))) (local.set $clen (i32.load (i32.const 0x7008))) (if (i32.gt_u (local.get $clen) (i32.const 0)) (then (if (i32.gt_u (i32.add (local.get $len) (local.get $clen)) (local.get $cap)) (then (local.set $ncap (local.get $cap)) (loop $g (if (i32.lt_u (local.get $ncap) (i32.add (local.get $len) (local.get $clen))) (then (local.set $ncap (i32.mul (local.get $ncap) (i32.const 2))) (br $g) )) ) (local.set $buf (call $realloc (local.get $buf) (local.get $len) (i32.const 1) (local.get $ncap))) (local.set $cap (local.get $ncap)) )) (if (local.get $strip_crlf) (then (local.set $i (i32.const 0)) (loop $copy_strip (if (i32.lt_u (local.get $i) (local.get $clen)) (then (local.set $b (i32.load8_u (i32.add (local.get $cptr) (local.get $i)))) (if (i32.and (i32.ne (local.get $b) (i32.const 10)) (i32.ne (local.get $b) (i32.const 13))) (then (i32.store8 (i32.add (local.get $buf) (local.get $len)) (local.get $b)) (local.set $len (i32.add (local.get $len) (i32.const 1))) )) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $copy_strip) )) ) ) (else (memory.copy (i32.add (local.get $buf) (local.get $len)) (local.get $cptr) (local.get $clen)) (local.set $len (i32.add (local.get $len) (local.get $clen))) ) ) )) (br $l) ) (else (if (i32.eq (i32.load8_u (i32.const 0x7004)) (i32.const 0)) (then (call $drop_io_error (i32.load (i32.const 0x7008))) (return (i32.const 1)) )) ;; se_tag == 1 -> closed (EOF) ) ) ) (global.set $ret_ptr (local.get $buf)) (global.set $ret_len (local.get $len)) (i32.const 0) ) (func $init_preopens (if (global.get $g_preopens_init) (then (return))) (call $get_directories (i32.const 0x7080)) (global.set $g_preopens_ptr (i32.load (i32.const 0x7080))) (global.set $g_preopens_cnt (i32.load (i32.const 0x7084))) (global.set $g_preopens_init (i32.const 1)) ) ;; Resolve path against WASI preopens and call open-at. Returns fd or -1. (func $resolve_and_open (param $path_ptr i32) (param $path_len i32) (param $open_flags i32) (param $desc_flags i32) (result i32) (local $cnt i32) (local $i i32) (local $entry i32) (local $gfd i32) (local $gp i32) (local $glen i32) (local $best_fd i32) (local $best_glen i32) (local $rem_p i32) (local $rem_l i32) (local $p i32) (local $len i32) (call $init_preopens) (local.set $cnt (global.get $g_preopens_cnt)) (if (i32.eq (local.get $cnt) (i32.const 0)) (then (return (i32.const -1)))) (if (i32.eq (local.get $path_len) (i32.const 0)) (then (return (i32.const -1)))) (local.set $p (local.get $path_ptr)) (local.set $len (local.get $path_len)) (local.set $best_fd (i32.const -1)) (if (i32.eq (i32.load8_u (local.get $p)) (i32.const 47)) ;; starts with '/' (then (loop $l_abs (if (i32.lt_u (local.get $i) (local.get $cnt)) (then (local.set $entry (i32.add (global.get $g_preopens_ptr) (i32.mul (local.get $i) (i32.const 12)))) (local.set $gfd (i32.load (local.get $entry))) (local.set $gp (i32.load (i32.add (local.get $entry) (i32.const 4)))) (local.set $glen (i32.load (i32.add (local.get $entry) (i32.const 8)))) ;; Strip trailing '/' from guest preopen path if glen > 1 (if (i32.and (i32.gt_u (local.get $glen) (i32.const 1)) (i32.eq (i32.load8_u (i32.add (local.get $gp) (i32.sub (local.get $glen) (i32.const 1)))) (i32.const 47))) (then (local.set $glen (i32.sub (local.get $glen) (i32.const 1)))) ) (if (i32.and (i32.eq (local.get $glen) (i32.const 1)) (i32.eq (i32.load8_u (local.get $gp)) (i32.const 47))) (then (if (i32.gt_u (local.get $glen) (local.get $best_glen)) (then (local.set $best_fd (local.get $gfd)) (local.set $best_glen (i32.const 1)) )) ) (else (if (i32.and (call $starts_with (local.get $p) (local.get $len) (local.get $gp) (local.get $glen)) (i32.or (i32.eq (local.get $len) (local.get $glen)) (i32.eq (i32.load8_u (i32.add (local.get $p) (local.get $glen))) (i32.const 47)))) (then (if (i32.gt_u (local.get $glen) (local.get $best_glen)) (then (local.set $best_fd (local.get $gfd)) (local.set $best_glen (local.get $glen)) )) ) ) ) ) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l_abs) )) ) (if (i32.eq (local.get $best_fd) (i32.const -1)) (then (return (i32.const -1)))) (local.set $rem_p (i32.add (local.get $p) (local.get $best_glen))) (local.set $rem_l (i32.sub (local.get $len) (local.get $best_glen))) (loop $skip_sl (if (i32.and (i32.gt_u (local.get $rem_l) (i32.const 0)) (i32.eq (i32.load8_u (local.get $rem_p)) (i32.const 47))) (then (local.set $rem_p (i32.add (local.get $rem_p) (i32.const 1))) (local.set $rem_l (i32.sub (local.get $rem_l) (i32.const 1))) (br $skip_sl) )) ) (if (i32.eq (local.get $rem_l) (i32.const 0)) (then (return (i32.const -1)))) ) (else ;; Relative path: strip leading "./" (local.set $rem_p (local.get $p)) (local.set $rem_l (local.get $len)) (loop $strip_dot (if (call $starts_with (local.get $rem_p) (local.get $rem_l) (i32.const 0x0A30) (i32.const 2)) (then (local.set $rem_p (i32.add (local.get $rem_p) (i32.const 2))) (local.set $rem_l (i32.sub (local.get $rem_l) (i32.const 2))) (br $strip_dot) )) ) (if (i32.eq (local.get $rem_l) (i32.const 0)) (then (return (i32.const -1)))) ;; Prefer preopen named "." or "./", else preopen 0 (local.set $best_fd (i32.load (global.get $g_preopens_ptr))) (loop $l_rel (if (i32.lt_u (local.get $i) (local.get $cnt)) (then (local.set $entry (i32.add (global.get $g_preopens_ptr) (i32.mul (local.get $i) (i32.const 12)))) (local.set $gp (i32.load (i32.add (local.get $entry) (i32.const 4)))) (local.set $glen (i32.load (i32.add (local.get $entry) (i32.const 8)))) (if (i32.or (call $memeq (local.get $gp) (local.get $glen) (i32.const 0x0A28) (i32.const 1)) (call $memeq (local.get $gp) (local.get $glen) (i32.const 0x0A30) (i32.const 2))) (then (local.set $best_fd (i32.load (local.get $entry))) (local.set $i (local.get $cnt)) ) ) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l_rel) )) ) ) ) ;; path_flags = 1 (symlink-follow) (call $desc_open_at (local.get $best_fd) (i32.const 1) (local.get $rem_p) (local.get $rem_l) (local.get $open_flags) (local.get $desc_flags) (i32.const 0x7080)) (if (i32.ne (i32.load8_u (i32.const 0x7080)) (i32.const 0)) (then (return (i32.const -1)))) (i32.load (i32.const 0x7084)) ) ;; Read file or "-" (stdin); sets $ret_ptr, $ret_len. Returns 0 ok, 1 err (func $read_file_or_stdin (param $path_ptr i32) (param $path_len i32) (param $strip_crlf i32) (result i32) (local $fd i32) (local $is i32) (local $rc i32) (if (call $memeq (local.get $path_ptr) (local.get $path_len) (i32.const 0x0A24) (i32.const 1)) (then (return (call $read_stream_all (global.get $g_stdin) (local.get $strip_crlf))) )) (local.set $fd (call $resolve_and_open (local.get $path_ptr) (local.get $path_len) (i32.const 0) (i32.const 1))) (if (i32.eq (local.get $fd) (i32.const -1)) (then (if (call $should_show_err) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0E08) (i32.const 59)) (call $write_err (local.get $path_ptr) (local.get $path_len)) (call $write_err (i32.const 0x0A00) (i32.const 1)) (drop (call $stream_flush (global.get $g_stderr))) )) (return (i32.const 1)) )) (call $desc_read_via_stream (local.get $fd) (i64.const 0) (i32.const 0x7080)) (if (i32.ne (i32.load8_u (i32.const 0x7080)) (i32.const 0)) (then (call $drop_descriptor (local.get $fd)) (if (call $should_show_err) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0E08) (i32.const 59)) (call $write_err (local.get $path_ptr) (local.get $path_len)) (call $write_err (i32.const 0x0A00) (i32.const 1)) (drop (call $stream_flush (global.get $g_stderr))) )) (return (i32.const 1)) )) (local.set $is (i32.load (i32.const 0x7084))) (local.set $rc (call $read_stream_all (local.get $is) (local.get $strip_crlf))) (call $drop_input_stream (local.get $is)) (call $drop_descriptor (local.get $fd)) (local.get $rc) ) ;; Open an output file ("-", "/dev/null", or path). Sets $ret_ptr=mode (0=stdout,1=file,2=null), $ret_len=fd, $ret_val3=stream. Returns 0 ok, 1 err. (func $open_output_sink (param $path_ptr i32) (param $path_len i32) (result i32) (local $fd i32) (local $os i32) (if (call $memeq (local.get $path_ptr) (local.get $path_len) (i32.const 0x0A24) (i32.const 1)) (then (global.set $ret_ptr (i32.const 0)) (global.set $ret_len (i32.const 0)) (global.set $ret_val3 (global.get $g_stdout)) (return (i32.const 0)) )) (if (call $memeq (local.get $path_ptr) (local.get $path_len) (i32.const 0x0A38) (i32.const 9)) (then (global.set $ret_ptr (i32.const 2)) (global.set $ret_len (i32.const 0)) (global.set $ret_val3 (i32.const 0)) (return (i32.const 0)) )) ;; open_flags = 9 (create | truncate), desc_flags = 2 (write) (local.set $fd (call $resolve_and_open (local.get $path_ptr) (local.get $path_len) (i32.const 9) (i32.const 2))) (if (i32.eq (local.get $fd) (i32.const -1)) (then (if (call $should_show_err) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0E48) (i32.const 55)) (call $write_err (local.get $path_ptr) (local.get $path_len)) (call $write_err (i32.const 0x0A00) (i32.const 1)) (drop (call $stream_flush (global.get $g_stderr))) )) (return (i32.const 1)) )) (call $desc_write_via_stream (local.get $fd) (i64.const 0) (i32.const 0x7080)) (if (i32.ne (i32.load8_u (i32.const 0x7080)) (i32.const 0)) (then (call $drop_descriptor (local.get $fd)) (if (call $should_show_err) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0E48) (i32.const 55)) (call $write_err (local.get $path_ptr) (local.get $path_len)) (call $write_err (i32.const 0x0A00) (i32.const 1)) (drop (call $stream_flush (global.get $g_stderr))) )) (return (i32.const 1)) )) (local.set $os (i32.load (i32.const 0x7084))) (global.set $ret_ptr (i32.const 1)) (global.set $ret_len (local.get $fd)) (global.set $ret_val3 (local.get $os)) (i32.const 0) ) ;; ========================================================================== ;; 4c. URL Parser, Query/Upload Path Mutators & Redirect Resolver ;; ========================================================================== (func $rebuild_url_eff (local $sp i32) (local $sl i32) (local $tot i32) (local $buf i32) (if (i32.eq (global.get $url_scheme) (i32.const 1)) (then (local.set $sp (i32.const 0x0A78)) (local.set $sl (i32.const 8))) (else (local.set $sp (i32.const 0x0A70)) (local.set $sl (i32.const 7))) ) (local.set $tot (i32.add (i32.add (local.get $sl) (global.get $url_auth_len)) (global.get $url_path_len))) (local.set $buf (call $alloc (local.get $tot) (i32.const 1))) (memory.copy (local.get $buf) (local.get $sp) (local.get $sl)) (memory.copy (i32.add (local.get $buf) (local.get $sl)) (global.get $url_auth_ptr) (global.get $url_auth_len)) (memory.copy (i32.add (local.get $buf) (i32.add (local.get $sl) (global.get $url_auth_len))) (global.get $url_path_ptr) (global.get $url_path_len)) (global.set $url_eff_ptr (local.get $buf)) (global.set $url_eff_len (local.get $tot)) ) ;; Parse URL into $url_* globals. Returns 0 ok, 1 error. (func $parse_url (param $p i32) (param $len i32) (result i32) (local $hash i32) (local $ssep i32) (local $off i32) (local $auth_end i32) (local $at i32) (local $host_p i32) (local $host_l i32) (local $path_p i32) (local $path_l i32) (local $tmp i32) (if (i32.eq (local.get $len) (i32.const 0)) (then (return (i32.const 1)))) ;; Strip #fragment (local.set $hash (call $find_byte (local.get $p) (local.get $len) (i32.const 35))) (if (i32.ne (local.get $hash) (i32.const -1)) (then (local.set $len (local.get $hash)) )) (if (i32.eq (local.get $len) (i32.const 0)) (then (return (i32.const 1)))) ;; Check scheme before '/' or '?' (local.set $ssep (call $find_scheme_sep (local.get $p) (local.get $len))) (local.set $tmp (call $find_bytes2 (local.get $p) (local.get $len) (i32.const 47) (i32.const 63))) (if (i32.and (i32.ne (local.get $ssep) (i32.const -1)) (i32.or (i32.eq (local.get $tmp) (i32.const -1)) (i32.lt_u (local.get $ssep) (local.get $tmp)))) (then (if (call $memeq_ci (local.get $p) (local.get $ssep) (i32.const 0x0A60) (i32.const 4)) (then (global.set $url_scheme (i32.const 0))) (else (if (call $memeq_ci (local.get $p) (local.get $ssep) (i32.const 0x0A68) (i32.const 5)) (then (global.set $url_scheme (i32.const 1))) (else (return (i32.const 1))) ) ) ) (local.set $off (i32.add (local.get $ssep) (i32.const 3))) ) (else (global.set $url_scheme (i32.const 0)) (local.set $off (i32.const 0)) ) ) ;; Authority runs from off until '/' or '?' or end (local.set $tmp (call $find_bytes2 (i32.add (local.get $p) (local.get $off)) (i32.sub (local.get $len) (local.get $off)) (i32.const 47) (i32.const 63))) (if (i32.eq (local.get $tmp) (i32.const -1)) (then (local.set $auth_end (local.get $len))) (else (local.set $auth_end (i32.add (local.get $off) (local.get $tmp)))) ) ;; Check userinfo@ inside authority (local.set $at (call $rfind_byte (i32.add (local.get $p) (local.get $off)) (i32.sub (local.get $auth_end) (local.get $off)) (i32.const 64))) (if (i32.ne (local.get $at) (i32.const -1)) (then (global.set $url_has_userinfo (i32.const 1)) (global.set $url_userinfo_ptr (call $heap_strdup (i32.add (local.get $p) (local.get $off)) (local.get $at))) (global.set $url_userinfo_len (local.get $at)) (local.set $host_p (i32.add (local.get $p) (i32.add (i32.add (local.get $off) (local.get $at)) (i32.const 1)))) (local.set $host_l (i32.sub (local.get $auth_end) (i32.add (i32.add (local.get $off) (local.get $at)) (i32.const 1)))) ) (else (global.set $url_has_userinfo (i32.const 0)) (global.set $url_userinfo_ptr (i32.const 0)) (global.set $url_userinfo_len (i32.const 0)) (local.set $host_p (i32.add (local.get $p) (local.get $off))) (local.set $host_l (i32.sub (local.get $auth_end) (local.get $off))) ) ) (if (i32.eq (local.get $host_l) (i32.const 0)) (then (return (i32.const 1)))) (global.set $url_auth_ptr (call $heap_strdup (local.get $host_p) (local.get $host_l))) (global.set $url_auth_len (local.get $host_l)) ;; Path with query (local.set $path_p (i32.add (local.get $p) (local.get $auth_end))) (local.set $path_l (i32.sub (local.get $len) (local.get $auth_end))) (if (i32.eq (local.get $path_l) (i32.const 0)) (then (global.set $url_path_ptr (i32.const 0x0A10)) (global.set $url_path_len (i32.const 1)) ) (else (if (i32.eq (i32.load8_u (local.get $path_p)) (i32.const 63)) ;; starts with '?' (then (local.set $tmp (call $alloc (i32.add (local.get $path_l) (i32.const 1)) (i32.const 1))) (i32.store8 (local.get $tmp) (i32.const 47)) (memory.copy (i32.add (local.get $tmp) (i32.const 1)) (local.get $path_p) (local.get $path_l)) (global.set $url_path_ptr (local.get $tmp)) (global.set $url_path_len (i32.add (local.get $path_l) (i32.const 1))) ) (else (global.set $url_path_ptr (call $heap_strdup (local.get $path_p) (local.get $path_l))) (global.set $url_path_len (local.get $path_l)) ) ) ) ) (call $rebuild_url_eff) (i32.const 0) ) ;; For -G: append query string to $url_path (func $append_query_to_url (param $qp i32) (param $ql i32) (local $has_q i32) (local $sep i32) (local $buf i32) (if (i32.eq (local.get $ql) (i32.const 0)) (then (return))) (local.set $has_q (i32.ne (call $find_byte (global.get $url_path_ptr) (global.get $url_path_len) (i32.const 63)) (i32.const -1))) (local.set $sep (select (i32.const 38) (i32.const 63) (local.get $has_q))) ;; '&' or '?' (local.set $buf (call $alloc (i32.add (i32.add (global.get $url_path_len) (i32.const 1)) (local.get $ql)) (i32.const 1))) (memory.copy (local.get $buf) (global.get $url_path_ptr) (global.get $url_path_len)) (i32.store8 (i32.add (local.get $buf) (global.get $url_path_len)) (local.get $sep)) (memory.copy (i32.add (local.get $buf) (i32.add (global.get $url_path_len) (i32.const 1))) (local.get $qp) (local.get $ql)) (global.set $url_path_ptr (local.get $buf)) (global.set $url_path_len (i32.add (i32.add (global.get $url_path_len) (i32.const 1)) (local.get $ql))) (call $rebuild_url_eff) ) ;; For -T: if $url_path's path part ends with '/', append basename of file_path (func $maybe_append_upload_basename (param $fp i32) (param $fl i32) (local $q i32) (local $plen i32) (local $sl i32) (local $bp i32) (local $bl i32) (local $buf i32) (if (call $memeq (local.get $fp) (local.get $fl) (i32.const 0x0A24) (i32.const 1)) (then (return))) (local.set $q (call $find_byte (global.get $url_path_ptr) (global.get $url_path_len) (i32.const 63))) (local.set $plen (select (global.get $url_path_len) (local.get $q) (i32.eq (local.get $q) (i32.const -1)))) (if (i32.eq (local.get $plen) (i32.const 0)) (then (return))) (if (i32.ne (i32.load8_u (i32.add (global.get $url_path_ptr) (i32.sub (local.get $plen) (i32.const 1)))) (i32.const 47)) (then (return)) ) (local.set $sl (call $rfind_byte (local.get $fp) (local.get $fl) (i32.const 47))) (if (i32.ne (local.get $sl) (i32.const -1)) (then (local.set $bp (i32.add (local.get $fp) (i32.add (local.get $sl) (i32.const 1)))) (local.set $bl (i32.sub (local.get $fl) (i32.add (local.get $sl) (i32.const 1)))) ) (else (local.set $bp (local.get $fp)) (local.set $bl (local.get $fl))) ) (if (i32.eq (local.get $bl) (i32.const 0)) (then (return))) (local.set $buf (call $alloc (i32.add (global.get $url_path_len) (local.get $bl)) (i32.const 1))) (memory.copy (local.get $buf) (global.get $url_path_ptr) (local.get $plen)) (memory.copy (i32.add (local.get $buf) (local.get $plen)) (local.get $bp) (local.get $bl)) (if (i32.lt_u (local.get $plen) (global.get $url_path_len)) (then (memory.copy (i32.add (local.get $buf) (i32.add (local.get $plen) (local.get $bl))) (i32.add (global.get $url_path_ptr) (local.get $plen)) (i32.sub (global.get $url_path_len) (local.get $plen)) ) )) (global.set $url_path_ptr (local.get $buf)) (global.set $url_path_len (i32.add (global.get $url_path_len) (local.get $bl))) (call $rebuild_url_eff) ) ;; For -O: extract remote filename from $url_path into $ret_ptr, $ret_len. Returns 0 ok, 1 err. (func $extract_remote_name (result i32) (local $q i32) (local $plen i32) (local $sl i32) (local $np i32) (local $nl i32) (local.set $q (call $find_byte (global.get $url_path_ptr) (global.get $url_path_len) (i32.const 63))) (local.set $plen (select (global.get $url_path_len) (local.get $q) (i32.eq (local.get $q) (i32.const -1)))) (local.set $sl (call $rfind_byte (global.get $url_path_ptr) (local.get $plen) (i32.const 47))) (if (i32.eq (local.get $sl) (i32.const -1)) (then (local.set $np (global.get $url_path_ptr)) (local.set $nl (local.get $plen))) (else (local.set $np (i32.add (global.get $url_path_ptr) (i32.add (local.get $sl) (i32.const 1)))) (local.set $nl (i32.sub (local.get $plen) (i32.add (local.get $sl) (i32.const 1)))) ) ) (if (i32.eq (local.get $nl) (i32.const 0)) (then (return (i32.const 1)))) (global.set $ret_ptr (local.get $np)) (global.set $ret_len (local.get $nl)) (i32.const 0) ) ;; Normalize dot segments of path at in_p[0..in_l] into BUF_URL+0x1000 (0xA000), returns normalized len (func $normalize_dot_segments (param $in_p i32) (param $in_l i32) (result i32) (local $out i32) (local $olen i32) (local $i i32) (local $seg_s i32) (local $seg_l i32) (local $has_sl i32) (local.set $out (i32.const 0xA000)) (i32.store8 (local.get $out) (i32.const 47)) (local.set $olen (i32.const 1)) (local.set $i (i32.const 0)) (if (i32.and (i32.gt_u (local.get $in_l) (i32.const 0)) (i32.eq (i32.load8_u (local.get $in_p)) (i32.const 47))) (then (local.set $i (i32.const 1)) )) (loop $l (if (i32.lt_u (local.get $i) (local.get $in_l)) (then (local.set $seg_s (local.get $i)) (loop $scan (if (i32.and (i32.lt_u (local.get $i) (local.get $in_l)) (i32.ne (i32.load8_u (i32.add (local.get $in_p) (local.get $i))) (i32.const 47))) (then (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $scan) ) ) ) (local.set $seg_l (i32.sub (local.get $i) (local.get $seg_s))) (local.set $has_sl (i32.and (i32.lt_u (local.get $i) (local.get $in_l)) (i32.eq (i32.load8_u (i32.add (local.get $in_p) (local.get $i))) (i32.const 47)))) (if (local.get $has_sl) (then (local.set $i (i32.add (local.get $i) (i32.const 1))))) (if (call $memeq (i32.add (local.get $in_p) (local.get $seg_s)) (local.get $seg_l) (i32.const 0x0A28) (i32.const 1)) (then ;; "." -> skip ) (else (if (call $memeq (i32.add (local.get $in_p) (local.get $seg_s)) (local.get $seg_l) (i32.const 0x0A2C) (i32.const 2)) (then ;; ".." -> pop one segment (if (i32.gt_u (local.get $olen) (i32.const 1)) (then (if (i32.eq (i32.load8_u (i32.add (local.get $out) (i32.sub (local.get $olen) (i32.const 1)))) (i32.const 47)) (then (local.set $olen (i32.sub (local.get $olen) (i32.const 1))) )) (loop $pop (if (i32.and (i32.gt_u (local.get $olen) (i32.const 1)) (i32.ne (i32.load8_u (i32.add (local.get $out) (i32.sub (local.get $olen) (i32.const 1)))) (i32.const 47))) (then (local.set $olen (i32.sub (local.get $olen) (i32.const 1))) (br $pop) ) ) ) )) ) (else (if (i32.gt_u (local.get $seg_l) (i32.const 0)) (then (if (i32.and (i32.gt_u (local.get $olen) (i32.const 0)) (i32.ne (i32.load8_u (i32.add (local.get $out) (i32.sub (local.get $olen) (i32.const 1)))) (i32.const 47))) (then (i32.store8 (i32.add (local.get $out) (local.get $olen)) (i32.const 47)) (local.set $olen (i32.add (local.get $olen) (i32.const 1))) ) ) (memory.copy (i32.add (local.get $out) (local.get $olen)) (i32.add (local.get $in_p) (local.get $seg_s)) (local.get $seg_l)) (local.set $olen (i32.add (local.get $olen) (local.get $seg_l))) (if (local.get $has_sl) (then (i32.store8 (i32.add (local.get $out) (local.get $olen)) (i32.const 47)) (local.set $olen (i32.add (local.get $olen) (i32.const 1))) )) )) ) ) ) ) (br $l) )) ) (local.get $olen) ) ;; Resolve redirect Location header against current URL, updating $url_* & $cross_origin_redir. Returns 0 ok, 1 err. (func $resolve_redirect (param $lp i32) (param $ll i32) (result i32) (local $old_scheme i32) (local $old_ap i32) (local $old_al i32) (local $ssep i32) (local $delim i32) (local $buf i32) (local $blen i32) (local $sp i32) (local $sl i32) (local $q i32) (local $base_l i32) (local $sl_pos i32) (local $hash i32) (local $rel_q i32) (local $rel_path_l i32) (local $raw_p i32) (local $raw_l i32) (local $norm_l i32) (call $trim_ws (local.get $lp) (local.get $ll)) (local.set $lp (global.get $ret_ptr)) (local.set $ll (global.get $ret_len)) (if (i32.eq (local.get $ll) (i32.const 0)) (then (return (i32.const 1)))) (local.set $old_scheme (global.get $url_scheme)) (local.set $old_ap (global.get $url_auth_ptr)) (local.set $old_al (global.get $url_auth_len)) ;; Strip #fragment from location (local.set $hash (call $find_byte (local.get $lp) (local.get $ll) (i32.const 35))) (if (i32.eq (local.get $hash) (i32.const 0)) (then ;; Fragment-only redirect -> keep same effective URL (return (i32.const 0)) )) (if (i32.ne (local.get $hash) (i32.const -1)) (then (local.set $ll (local.get $hash)) )) ;; 1. Full URL? (local.set $ssep (call $find_scheme_sep (local.get $lp) (local.get $ll))) (local.set $delim (call $find_bytes2 (local.get $lp) (local.get $ll) (i32.const 47) (i32.const 63))) (if (i32.and (i32.ne (local.get $ssep) (i32.const -1)) (i32.or (i32.eq (local.get $delim) (i32.const -1)) (i32.lt_u (local.get $ssep) (local.get $delim)))) (then (if (call $parse_url (local.get $lp) (local.get $ll)) (then (return (i32.const 1)))) (if (i32.or (i32.ne (global.get $url_scheme) (local.get $old_scheme)) (i32.eqz (call $memeq_ci (global.get $url_auth_ptr) (global.get $url_auth_len) (local.get $old_ap) (local.get $old_al)))) (then (global.set $cross_origin_redir (i32.const 1))) ) (return (i32.const 0)) ) ) ;; 2. Protocol-relative "//..." (if (i32.and (i32.ge_u (local.get $ll) (i32.const 2)) (i32.and (i32.eq (i32.load8_u (local.get $lp)) (i32.const 47)) (i32.eq (i32.load8_u (i32.add (local.get $lp) (i32.const 1))) (i32.const 47)))) (then (if (i32.eq (local.get $old_scheme) (i32.const 1)) (then (local.set $sp (i32.const 0x0A68)) (local.set $sl (i32.const 5))) ;; "https" (else (local.set $sp (i32.const 0x0A60)) (local.set $sl (i32.const 4))) ;; "http" ) (local.set $buf (call $alloc (i32.add (i32.add (local.get $sl) (i32.const 1)) (local.get $ll)) (i32.const 1))) (memory.copy (local.get $buf) (local.get $sp) (local.get $sl)) (i32.store8 (i32.add (local.get $buf) (local.get $sl)) (i32.const 58)) ;; ':' (memory.copy (i32.add (local.get $buf) (i32.add (local.get $sl) (i32.const 1))) (local.get $lp) (local.get $ll)) (if (call $parse_url (local.get $buf) (i32.add (i32.add (local.get $sl) (i32.const 1)) (local.get $ll))) (then (return (i32.const 1))) ) (if (i32.eqz (call $memeq_ci (global.get $url_auth_ptr) (global.get $url_auth_len) (local.get $old_ap) (local.get $old_al))) (then (global.set $cross_origin_redir (i32.const 1))) ) (return (i32.const 0)) ) ) ;; 3. Root-relative "/..." (if (i32.eq (i32.load8_u (local.get $lp)) (i32.const 47)) (then (global.set $url_path_ptr (call $heap_strdup (local.get $lp) (local.get $ll))) (global.set $url_path_len (local.get $ll)) (call $rebuild_url_eff) (return (i32.const 0)) )) ;; Base path without query (local.set $q (call $find_byte (global.get $url_path_ptr) (global.get $url_path_len) (i32.const 63))) (local.set $base_l (select (global.get $url_path_len) (local.get $q) (i32.eq (local.get $q) (i32.const -1)))) ;; 4. Query-relative "?..." (if (i32.eq (i32.load8_u (local.get $lp)) (i32.const 63)) (then (local.set $buf (call $alloc (i32.add (local.get $base_l) (local.get $ll)) (i32.const 1))) (memory.copy (local.get $buf) (global.get $url_path_ptr) (local.get $base_l)) (memory.copy (i32.add (local.get $buf) (local.get $base_l)) (local.get $lp) (local.get $ll)) (global.set $url_path_ptr (local.get $buf)) (global.set $url_path_len (i32.add (local.get $base_l) (local.get $ll))) (call $rebuild_url_eff) (return (i32.const 0)) )) ;; 5. Path-relative (possibly with '.' / '..' and '?query') (local.set $sl_pos (call $rfind_byte (global.get $url_path_ptr) (local.get $base_l) (i32.const 47))) (local.set $base_l (select (i32.const 1) (i32.add (local.get $sl_pos) (i32.const 1)) (i32.eq (local.get $sl_pos) (i32.const -1)))) (local.set $rel_q (call $find_byte (local.get $lp) (local.get $ll) (i32.const 63))) (local.set $rel_path_l (select (local.get $ll) (local.get $rel_q) (i32.eq (local.get $rel_q) (i32.const -1)))) (local.set $raw_l (i32.add (local.get $base_l) (local.get $rel_path_l))) (local.set $raw_p (call $alloc (local.get $raw_l) (i32.const 1))) (memory.copy (local.get $raw_p) (global.get $url_path_ptr) (local.get $base_l)) (memory.copy (i32.add (local.get $raw_p) (local.get $base_l)) (local.get $lp) (local.get $rel_path_l)) (local.set $norm_l (call $normalize_dot_segments (local.get $raw_p) (local.get $raw_l))) (local.set $blen (i32.add (local.get $norm_l) (i32.sub (local.get $ll) (local.get $rel_path_l)))) (local.set $buf (call $alloc (local.get $blen) (i32.const 1))) (memory.copy (local.get $buf) (i32.const 0xA000) (local.get $norm_l)) (if (i32.lt_u (local.get $rel_path_l) (local.get $ll)) (then (memory.copy (i32.add (local.get $buf) (local.get $norm_l)) (i32.add (local.get $lp) (local.get $rel_path_l)) (i32.sub (local.get $ll) (local.get $rel_path_l)) ) )) (global.set $url_path_ptr (local.get $buf)) (global.set $url_path_len (local.get $blen)) (call $rebuild_url_eff) (i32.const 0) ) ;; ========================================================================== ;; 4d. CLI Option & Argument Parser ;; ========================================================================== (func $add_cli_header (param $p i32) (param $len i32) (local $colon i32) (local $semi i32) (local $np i32) (local $nl i32) (local $vp i32) (local $vl i32) (local $flags i32) (local $slot i32) (if (i32.ge_u (global.get $cli_hdr_cnt) (i32.const 128)) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x1070) (i32.const 27)) (return) )) (local.set $colon (call $find_byte (local.get $p) (local.get $len) (i32.const 58))) (if (i32.ne (local.get $colon) (i32.const -1)) (then (call $trim_ws (local.get $p) (local.get $colon)) (local.set $np (global.get $ret_ptr)) (local.set $nl (global.get $ret_len)) (if (i32.eq (local.get $nl) (i32.const 0)) (then (return))) (call $trim_ws (i32.add (local.get $p) (i32.add (local.get $colon) (i32.const 1))) (i32.sub (local.get $len) (i32.add (local.get $colon) (i32.const 1)))) (local.set $vp (global.get $ret_ptr)) (local.set $vl (global.get $ret_len)) (if (i32.eq (local.get $vl) (i32.const 0)) (then (local.set $flags (i32.const 1)) ;; suppress header )) ) (else (local.set $semi (call $find_byte (local.get $p) (local.get $len) (i32.const 59))) (if (i32.eq (local.get $semi) (i32.const -1)) (then (return))) (call $trim_ws (local.get $p) (local.get $semi)) (local.set $np (global.get $ret_ptr)) (local.set $nl (global.get $ret_len)) (if (i32.eq (local.get $nl) (i32.const 0)) (then (return))) (local.set $vp (i32.const 0x0A00)) (local.set $vl (i32.const 0)) (local.set $flags (i32.const 0)) ;; empty header value ) ) (local.set $slot (i32.add (i32.const 0x7400) (i32.mul (global.get $cli_hdr_cnt) (i32.const 20)))) (i32.store (local.get $slot) (local.get $np)) (i32.store (i32.add (local.get $slot) (i32.const 4)) (local.get $nl)) (i32.store (i32.add (local.get $slot) (i32.const 8)) (local.get $vp)) (i32.store (i32.add (local.get $slot) (i32.const 12)) (local.get $vl)) (i32.store (i32.add (local.get $slot) (i32.const 16)) (local.get $flags)) (global.set $cli_hdr_cnt (i32.add (global.get $cli_hdr_cnt) (i32.const 1))) ) ;; Check if CLI headers contain name_ptr[0..name_len] (case-insensitive) (func $cli_has_header_named (param $np i32) (param $nl i32) (result i32) (local $i i32) (local $slot i32) (loop $l (if (i32.lt_u (local.get $i) (global.get $cli_hdr_cnt)) (then (local.set $slot (i32.add (i32.const 0x7400) (i32.mul (local.get $i) (i32.const 20)))) (if (call $memeq_ci (i32.load (local.get $slot)) (i32.load (i32.add (local.get $slot) (i32.const 4))) (local.get $np) (local.get $nl)) (then (return (i32.const 1))) ) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l) )) ) (i32.const 0) ) ;; mode: 0=ascii (@file strips CRLF), 1=raw, 2=binary (@file raw) (func $append_data_opt (param $vp i32) (param $vl i32) (param $mode i32) (result i32) (if (global.get $has_data) (then (call $data_buf_append_byte (i32.const 38)) ;; '&' )) (global.set $has_data (i32.const 1)) (if (i32.and (i32.ne (local.get $mode) (i32.const 1)) (i32.and (i32.gt_u (local.get $vl) (i32.const 0)) (i32.eq (i32.load8_u (local.get $vp)) (i32.const 64)))) ;; '@' (then (if (call $read_file_or_stdin (i32.add (local.get $vp) (i32.const 1)) (i32.sub (local.get $vl) (i32.const 1)) (i32.eq (local.get $mode) (i32.const 0))) (then (return (i32.const 1))) ) (call $data_buf_append_bytes (global.get $ret_ptr) (global.get $ret_len)) ) (else (call $data_buf_append_bytes (local.get $vp) (local.get $vl)) ) ) (i32.const 0) ) ;; --data-urlencode (func $append_data_urlencode (param $vp i32) (param $vl i32) (result i32) (local $eq i32) (local $at i32) (if (global.get $has_data) (then (call $data_buf_append_byte (i32.const 38)) ;; '&' )) (global.set $has_data (i32.const 1)) (local.set $eq (call $find_byte (local.get $vp) (local.get $vl) (i32.const 61))) ;; '=' (local.set $at (call $find_byte (local.get $vp) (local.get $vl) (i32.const 64))) ;; '@' (if (i32.and (i32.ne (local.get $eq) (i32.const -1)) (i32.or (i32.eq (local.get $at) (i32.const -1)) (i32.lt_u (local.get $eq) (local.get $at)))) (then (if (i32.eq (local.get $eq) (i32.const 0)) (then (call $data_buf_append_urlencoded (i32.add (local.get $vp) (i32.const 1)) (i32.sub (local.get $vl) (i32.const 1))) ) (else (call $data_buf_append_bytes (local.get $vp) (i32.add (local.get $eq) (i32.const 1))) (call $data_buf_append_urlencoded (i32.add (local.get $vp) (i32.add (local.get $eq) (i32.const 1))) (i32.sub (local.get $vl) (i32.add (local.get $eq) (i32.const 1)))) ) ) (return (i32.const 0)) ) ) (if (i32.ne (local.get $at) (i32.const -1)) (then (if (call $read_file_or_stdin (i32.add (local.get $vp) (i32.add (local.get $at) (i32.const 1))) (i32.sub (local.get $vl) (i32.add (local.get $at) (i32.const 1))) (i32.const 0)) (then (return (i32.const 1))) ) (if (i32.gt_u (local.get $at) (i32.const 0)) (then (call $data_buf_append_bytes (local.get $vp) (local.get $at)) (call $data_buf_append_byte (i32.const 61)) )) (call $data_buf_append_urlencoded (global.get $ret_ptr) (global.get $ret_len)) (return (i32.const 0)) )) (call $data_buf_append_urlencoded (local.get $vp) (local.get $vl)) (i32.const 0) ) (func $err_unknown_opt (param $p i32) (param $len i32) (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0D50) (i32.const 7)) (call $write_err (local.get $p) (local.get $len)) (call $write_err (i32.const 0x0D58) (i32.const 13)) (drop (call $stream_flush (global.get $g_stderr))) ) (func $err_missing_param (param $p i32) (param $len i32) (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0D50) (i32.const 7)) (call $write_err (local.get $p) (local.get $len)) (call $write_err (i32.const 0x0D68) (i32.const 21)) (drop (call $stream_flush (global.get $g_stderr))) ) (func $err_invalid_num (param $p i32) (param $len i32) (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x1048) (i32.const 33)) (call $write_err (local.get $p) (local.get $len)) (call $write_err (i32.const 0x0A00) (i32.const 1)) (drop (call $stream_flush (global.get $g_stderr))) ) ;; Apply a value option identified by code: ;; 1=request 2=header 3=data-ascii 4=data-raw 5=data-binary 6=data-urlencode ;; 7=upload-file 8=max-redirs 9=output 10=dump-header 11=user 12=user-agent ;; 13=referer 14=cookie 15=range 16=write-out 17=connect-timeout 18=max-time (func $apply_val_opt (param $code i32) (param $vp i32) (param $vl i32) (param $op i32) (param $ol i32) (result i32) (if (i32.eq (local.get $code) (i32.const 1)) (then (global.set $opt_method_ptr (local.get $vp)) (global.set $opt_method_len (local.get $vl)) (return (i32.const 0)) )) (if (i32.eq (local.get $code) (i32.const 2)) (then (call $add_cli_header (local.get $vp) (local.get $vl)) (return (i32.const 0)) )) (if (i32.eq (local.get $code) (i32.const 3)) (then (return (call $append_data_opt (local.get $vp) (local.get $vl) (i32.const 0))) )) (if (i32.eq (local.get $code) (i32.const 4)) (then (return (call $append_data_opt (local.get $vp) (local.get $vl) (i32.const 1))) )) (if (i32.eq (local.get $code) (i32.const 5)) (then (return (call $append_data_opt (local.get $vp) (local.get $vl) (i32.const 2))) )) (if (i32.eq (local.get $code) (i32.const 6)) (then (return (call $append_data_urlencode (local.get $vp) (local.get $vl))) )) (if (i32.eq (local.get $code) (i32.const 7)) (then (global.set $opt_upload_set (i32.const 1)) (global.set $opt_upload_ptr (local.get $vp)) (global.set $opt_upload_len (local.get $vl)) (return (i32.const 0)) )) (if (i32.eq (local.get $code) (i32.const 8)) (then (if (call $parse_i32 (local.get $vp) (local.get $vl)) (then (call $err_invalid_num (local.get $op) (local.get $ol)) (return (i32.const 1)))) (global.set $opt_max_redirs (global.get $ret_ptr)) (return (i32.const 0)) )) (if (i32.eq (local.get $code) (i32.const 9)) (then (global.set $opt_output_set (i32.const 1)) (global.set $opt_output_ptr (local.get $vp)) (global.set $opt_output_len (local.get $vl)) (global.set $opt_remote_name (i32.const 0)) (return (i32.const 0)) )) (if (i32.eq (local.get $code) (i32.const 10)) (then (global.set $opt_dump_hdr_set (i32.const 1)) (global.set $opt_dump_hdr_ptr (local.get $vp)) (global.set $opt_dump_hdr_len (local.get $vl)) (return (i32.const 0)) )) (if (i32.eq (local.get $code) (i32.const 11)) (then (global.set $opt_user_set (i32.const 1)) (global.set $opt_user_ptr (local.get $vp)) (global.set $opt_user_len (local.get $vl)) (return (i32.const 0)) )) (if (i32.eq (local.get $code) (i32.const 12)) (then (global.set $opt_ua_set (i32.const 1)) (global.set $opt_ua_ptr (local.get $vp)) (global.set $opt_ua_len (local.get $vl)) (return (i32.const 0)) )) (if (i32.eq (local.get $code) (i32.const 13)) (then (global.set $opt_referer_set (i32.const 1)) (global.set $opt_referer_ptr (local.get $vp)) (global.set $opt_referer_len (local.get $vl)) (return (i32.const 0)) )) (if (i32.eq (local.get $code) (i32.const 14)) (then (global.set $opt_cookie_set (i32.const 1)) (global.set $opt_cookie_ptr (local.get $vp)) (global.set $opt_cookie_len (local.get $vl)) (return (i32.const 0)) )) (if (i32.eq (local.get $code) (i32.const 15)) (then (global.set $opt_range_set (i32.const 1)) (global.set $opt_range_ptr (local.get $vp)) (global.set $opt_range_len (local.get $vl)) (return (i32.const 0)) )) (if (i32.eq (local.get $code) (i32.const 16)) (then (global.set $opt_write_out_set (i32.const 1)) (global.set $opt_write_out_ptr (local.get $vp)) (global.set $opt_write_out_len (local.get $vl)) (return (i32.const 0)) )) (if (i32.eq (local.get $code) (i32.const 17)) (then (if (call $parse_seconds_to_ns (local.get $vp) (local.get $vl)) (then (call $err_invalid_num (local.get $op) (local.get $ol)) (return (i32.const 1)))) (global.set $opt_connect_timeout_set (i32.const 1)) (global.set $opt_connect_timeout_ns (global.get $tmp_u64)) (return (i32.const 0)) )) (if (i32.eq (local.get $code) (i32.const 18)) (then (if (call $parse_seconds_to_ns (local.get $vp) (local.get $vl)) (then (call $err_invalid_num (local.get $op) (local.get $ol)) (return (i32.const 1)))) (global.set $opt_max_time_set (i32.const 1)) (global.set $opt_max_time_ns (global.get $tmp_u64)) (return (i32.const 0)) )) (i32.const 1) ) ;; Match long option name -> returns value opt code (1..18) or 0 if not a value opt (func $match_long_val_opt (param $np i32) (param $nl i32) (result i32) (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1614) (i32.const 9)) (then (return (i32.const 1)))) ;; --request (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1620) (i32.const 8)) (then (return (i32.const 2)))) ;; --header (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x162C) (i32.const 6)) (then (return (i32.const 3)))) ;; --data (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1634) (i32.const 12)) (then (return (i32.const 3)))) ;; --data-ascii (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1644) (i32.const 10)) (then (return (i32.const 4)))) ;; --data-raw (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1650) (i32.const 13)) (then (return (i32.const 5)))) ;; --data-binary (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1660) (i32.const 16)) (then (return (i32.const 6)))) ;; --data-urlencode (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x167C) (i32.const 13)) (then (return (i32.const 7)))) ;; --upload-file (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x16AC) (i32.const 12)) (then (return (i32.const 8)))) ;; --max-redirs (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x16BC) (i32.const 8)) (then (return (i32.const 9)))) ;; --output (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x16D8) (i32.const 13)) (then (return (i32.const 10)))) ;; --dump-header (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1718) (i32.const 6)) (then (return (i32.const 11)))) ;; --user (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1720) (i32.const 12)) (then (return (i32.const 12)))) ;; --user-agent (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1730) (i32.const 9)) (then (return (i32.const 13)))) ;; --referer (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x173C) (i32.const 8)) (then (return (i32.const 14)))) ;; --cookie (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1748) (i32.const 7)) (then (return (i32.const 15)))) ;; --range (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1754) (i32.const 11)) (then (return (i32.const 16)))) ;; --write-out (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1764) (i32.const 17)) (then (return (i32.const 17)))) ;; --connect-timeout (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1778) (i32.const 10)) (then (return (i32.const 18)))) ;; --max-time (i32.const 0) ) ;; Map short flag char -> value opt code (1..18) or 0 (func $match_short_val_opt (param $c i32) (result i32) (if (i32.eq (local.get $c) (i32.const 88)) (then (return (i32.const 1)))) ;; 'X' (if (i32.eq (local.get $c) (i32.const 72)) (then (return (i32.const 2)))) ;; 'H' (if (i32.eq (local.get $c) (i32.const 100)) (then (return (i32.const 3)))) ;; 'd' (if (i32.eq (local.get $c) (i32.const 84)) (then (return (i32.const 7)))) ;; 'T' (if (i32.eq (local.get $c) (i32.const 111)) (then (return (i32.const 9)))) ;; 'o' (if (i32.eq (local.get $c) (i32.const 68)) (then (return (i32.const 10)))) ;; 'D' (if (i32.eq (local.get $c) (i32.const 117)) (then (return (i32.const 11)))) ;; 'u' (if (i32.eq (local.get $c) (i32.const 65)) (then (return (i32.const 12)))) ;; 'A' (if (i32.eq (local.get $c) (i32.const 101)) (then (return (i32.const 13)))) ;; 'e' (if (i32.eq (local.get $c) (i32.const 98)) (then (return (i32.const 14)))) ;; 'b' (if (i32.eq (local.get $c) (i32.const 114)) (then (return (i32.const 15)))) ;; 'r' (if (i32.eq (local.get $c) (i32.const 119)) (then (return (i32.const 16)))) ;; 'w' (if (i32.eq (local.get $c) (i32.const 109)) (then (return (i32.const 18)))) ;; 'm' (i32.const 0) ) (func $add_cli_url (param $p i32) (param $len i32) (result i32) (local $slot i32) (if (i32.ge_u (global.get $cli_url_cnt) (i32.const 64)) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x1090) (i32.const 23)) (drop (call $stream_flush (global.get $g_stderr))) (return (i32.const 1)) )) (local.set $slot (i32.add (i32.const 0x8000) (i32.mul (global.get $cli_url_cnt) (i32.const 8)))) (i32.store (local.get $slot) (local.get $p)) (i32.store (i32.add (local.get $slot) (i32.const 4)) (local.get $len)) (global.set $cli_url_cnt (i32.add (global.get $cli_url_cnt) (i32.const 1))) (i32.const 0) ) (func $parse_args (result i32) (local $args_p i32) (local $argc i32) (local $i i32) (local $ap i32) (local $al i32) (local $after_dd i32) (local $eq i32) (local $np i32) (local $nl i32) (local $vp i32) (local $vl i32) (local $has_eq i32) (local $code i32) (local $pos i32) (local $ch i32) (call $get_arguments (i32.const 0x70C0)) (local.set $args_p (i32.load (i32.const 0x70C0))) (local.set $argc (i32.load (i32.const 0x70C4))) (local.set $i (i32.const 1)) (loop $arg_loop (if (i32.lt_u (local.get $i) (local.get $argc)) (then (local.set $ap (i32.load (i32.add (local.get $args_p) (i32.mul (local.get $i) (i32.const 8))))) (local.set $al (i32.load (i32.add (local.get $args_p) (i32.add (i32.mul (local.get $i) (i32.const 8)) (i32.const 4))))) (if (i32.and (i32.eqz (local.get $after_dd)) (i32.and (i32.eq (local.get $al) (i32.const 2)) (i32.and (i32.eq (i32.load8_u (local.get $ap)) (i32.const 45)) (i32.eq (i32.load8_u (i32.add (local.get $ap) (i32.const 1))) (i32.const 45))))) (then (local.set $after_dd (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $arg_loop) ) ) (if (i32.and (i32.eqz (local.get $after_dd)) (i32.and (i32.ge_u (local.get $al) (i32.const 2)) (i32.eq (i32.load8_u (local.get $ap)) (i32.const 45)))) (then (if (i32.eq (i32.load8_u (i32.add (local.get $ap) (i32.const 1))) (i32.const 45)) (then ;; Long option "--..." (local.set $eq (call $find_byte (local.get $ap) (local.get $al) (i32.const 61))) (if (i32.ne (local.get $eq) (i32.const -1)) (then (local.set $has_eq (i32.const 1)) (local.set $np (local.get $ap)) (local.set $nl (local.get $eq)) (local.set $vp (i32.add (local.get $ap) (i32.add (local.get $eq) (i32.const 1)))) (local.set $vl (i32.sub (local.get $al) (i32.add (local.get $eq) (i32.const 1)))) ) (else (local.set $has_eq (i32.const 0)) (local.set $np (local.get $ap)) (local.set $nl (local.get $al)) ) ) ;; Check boolean long flags (only valid without = or reject) (if (i32.eqz (local.get $has_eq)) (then (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1600) (i32.const 6)) (then (global.set $opt_help (i32.const 1)) (return (i32.const 0)))) (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1608) (i32.const 9)) (then (global.set $opt_version (i32.const 1)) (return (i32.const 0)))) (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1674) (i32.const 5)) (then (global.set $opt_get (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $arg_loop))) (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x168C) (i32.const 9)) (then (global.set $opt_include (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $arg_loop))) (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1698) (i32.const 6)) (then (global.set $opt_head (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $arg_loop))) (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x16A0) (i32.const 10)) (then (global.set $opt_location (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $arg_loop))) (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x16C8) (i32.const 13)) (then (global.set $opt_remote_name (i32.const 1)) (global.set $opt_output_set (i32.const 0)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $arg_loop))) (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x16E8) (i32.const 8)) (then (global.set $opt_silent (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $arg_loop))) (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x16F4) (i32.const 12)) (then (global.set $opt_show_error (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $arg_loop))) (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1704) (i32.const 9)) (then (global.set $opt_verbose (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $arg_loop))) (if (call $memeq (local.get $np) (local.get $nl) (i32.const 0x1710) (i32.const 6)) (then (global.set $opt_fail (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $arg_loop))) )) (local.set $code (call $match_long_val_opt (local.get $np) (local.get $nl))) (if (i32.eq (local.get $code) (i32.const 0)) (then (call $err_unknown_opt (local.get $ap) (local.get $al)) (return (i32.const 1)) )) (if (i32.eqz (local.get $has_eq)) (then (local.set $i (i32.add (local.get $i) (i32.const 1))) (if (i32.ge_u (local.get $i) (local.get $argc)) (then (call $err_missing_param (local.get $np) (local.get $nl)) (return (i32.const 1)) )) (local.set $vp (i32.load (i32.add (local.get $args_p) (i32.mul (local.get $i) (i32.const 8))))) (local.set $vl (i32.load (i32.add (local.get $args_p) (i32.add (i32.mul (local.get $i) (i32.const 8)) (i32.const 4))))) )) (if (call $apply_val_opt (local.get $code) (local.get $vp) (local.get $vl) (local.get $np) (local.get $nl)) (then (return (i32.const 1)) )) ) (else ;; Short option cluster "-..." (local.set $pos (i32.const 1)) (loop $short_loop (if (i32.lt_u (local.get $pos) (local.get $al)) (then (local.set $ch (i32.load8_u (i32.add (local.get $ap) (local.get $pos)))) (if (i32.eq (local.get $ch) (i32.const 104)) (then (global.set $opt_help (i32.const 1)) (return (i32.const 0)))) ;; -h (if (i32.eq (local.get $ch) (i32.const 86)) (then (global.set $opt_version (i32.const 1)) (return (i32.const 0)))) ;; -V (if (i32.eq (local.get $ch) (i32.const 71)) (then (global.set $opt_get (i32.const 1)) (local.set $pos (i32.add (local.get $pos) (i32.const 1))) (br $short_loop))) ;; -G (if (i32.eq (local.get $ch) (i32.const 105)) (then (global.set $opt_include (i32.const 1)) (local.set $pos (i32.add (local.get $pos) (i32.const 1))) (br $short_loop))) ;; -i (if (i32.eq (local.get $ch) (i32.const 73)) (then (global.set $opt_head (i32.const 1)) (local.set $pos (i32.add (local.get $pos) (i32.const 1))) (br $short_loop))) ;; -I (if (i32.eq (local.get $ch) (i32.const 76)) (then (global.set $opt_location (i32.const 1)) (local.set $pos (i32.add (local.get $pos) (i32.const 1))) (br $short_loop))) ;; -L (if (i32.eq (local.get $ch) (i32.const 79)) (then (global.set $opt_remote_name (i32.const 1)) (global.set $opt_output_set (i32.const 0)) (local.set $pos (i32.add (local.get $pos) (i32.const 1))) (br $short_loop))) ;; -O (if (i32.eq (local.get $ch) (i32.const 115)) (then (global.set $opt_silent (i32.const 1)) (local.set $pos (i32.add (local.get $pos) (i32.const 1))) (br $short_loop))) ;; -s (if (i32.eq (local.get $ch) (i32.const 83)) (then (global.set $opt_show_error (i32.const 1)) (local.set $pos (i32.add (local.get $pos) (i32.const 1))) (br $short_loop))) ;; -S (if (i32.eq (local.get $ch) (i32.const 118)) (then (global.set $opt_verbose (i32.const 1)) (local.set $pos (i32.add (local.get $pos) (i32.const 1))) (br $short_loop))) ;; -v (if (i32.eq (local.get $ch) (i32.const 102)) (then (global.set $opt_fail (i32.const 1)) (local.set $pos (i32.add (local.get $pos) (i32.const 1))) (br $short_loop))) ;; -f ;; Prepare "-c" in 0x8400 for error messages (i32.store8 (i32.const 0x8400) (i32.const 45)) (i32.store8 (i32.const 0x8401) (local.get $ch)) (local.set $code (call $match_short_val_opt (local.get $ch))) (if (i32.eq (local.get $code) (i32.const 0)) (then (call $err_unknown_opt (i32.const 0x8400) (i32.const 2)) (return (i32.const 1)) )) (if (i32.lt_u (i32.add (local.get $pos) (i32.const 1)) (local.get $al)) (then (local.set $vp (i32.add (local.get $ap) (i32.add (local.get $pos) (i32.const 1)))) (local.set $vl (i32.sub (local.get $al) (i32.add (local.get $pos) (i32.const 1)))) ) (else (local.set $i (i32.add (local.get $i) (i32.const 1))) (if (i32.ge_u (local.get $i) (local.get $argc)) (then (call $err_missing_param (i32.const 0x8400) (i32.const 2)) (return (i32.const 1)) )) (local.set $vp (i32.load (i32.add (local.get $args_p) (i32.mul (local.get $i) (i32.const 8))))) (local.set $vl (i32.load (i32.add (local.get $args_p) (i32.add (i32.mul (local.get $i) (i32.const 8)) (i32.const 4))))) ) ) (if (call $apply_val_opt (local.get $code) (local.get $vp) (local.get $vl) (i32.const 0x8400) (i32.const 2)) (then (return (i32.const 1)) )) (local.set $pos (local.get $al)) )) ) ) ) ) (else (if (call $add_cli_url (local.get $ap) (local.get $al)) (then (return (i32.const 1)))) ) ) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $arg_loop) )) ) (i32.const 0) ) ;; ========================================================================== ;; 4e. HTTP Helpers (Status Reason, Error Mapping, Headers) ;; ========================================================================== (func $lookup_reason (param $s i32) (global.set $ret_len (i32.const 0)) (if (i32.eq (local.get $s) (i32.const 100)) (then (global.set $ret_ptr (i32.const 0x1200)) (global.set $ret_len (i32.const 8)) (return))) (if (i32.eq (local.get $s) (i32.const 101)) (then (global.set $ret_ptr (i32.const 0x1210)) (global.set $ret_len (i32.const 19)) (return))) (if (i32.eq (local.get $s) (i32.const 200)) (then (global.set $ret_ptr (i32.const 0x1228)) (global.set $ret_len (i32.const 2)) (return))) (if (i32.eq (local.get $s) (i32.const 201)) (then (global.set $ret_ptr (i32.const 0x1230)) (global.set $ret_len (i32.const 7)) (return))) (if (i32.eq (local.get $s) (i32.const 202)) (then (global.set $ret_ptr (i32.const 0x1238)) (global.set $ret_len (i32.const 8)) (return))) (if (i32.eq (local.get $s) (i32.const 204)) (then (global.set $ret_ptr (i32.const 0x1248)) (global.set $ret_len (i32.const 10)) (return))) (if (i32.eq (local.get $s) (i32.const 206)) (then (global.set $ret_ptr (i32.const 0x1258)) (global.set $ret_len (i32.const 15)) (return))) (if (i32.eq (local.get $s) (i32.const 300)) (then (global.set $ret_ptr (i32.const 0x1268)) (global.set $ret_len (i32.const 16)) (return))) (if (i32.eq (local.get $s) (i32.const 301)) (then (global.set $ret_ptr (i32.const 0x1280)) (global.set $ret_len (i32.const 17)) (return))) (if (i32.eq (local.get $s) (i32.const 302)) (then (global.set $ret_ptr (i32.const 0x1298)) (global.set $ret_len (i32.const 5)) (return))) (if (i32.eq (local.get $s) (i32.const 303)) (then (global.set $ret_ptr (i32.const 0x12A0)) (global.set $ret_len (i32.const 9)) (return))) (if (i32.eq (local.get $s) (i32.const 304)) (then (global.set $ret_ptr (i32.const 0x12B0)) (global.set $ret_len (i32.const 12)) (return))) (if (i32.eq (local.get $s) (i32.const 307)) (then (global.set $ret_ptr (i32.const 0x12C0)) (global.set $ret_len (i32.const 18)) (return))) (if (i32.eq (local.get $s) (i32.const 308)) (then (global.set $ret_ptr (i32.const 0x12D8)) (global.set $ret_len (i32.const 18)) (return))) (if (i32.eq (local.get $s) (i32.const 400)) (then (global.set $ret_ptr (i32.const 0x12F0)) (global.set $ret_len (i32.const 11)) (return))) (if (i32.eq (local.get $s) (i32.const 401)) (then (global.set $ret_ptr (i32.const 0x1300)) (global.set $ret_len (i32.const 12)) (return))) (if (i32.eq (local.get $s) (i32.const 403)) (then (global.set $ret_ptr (i32.const 0x1310)) (global.set $ret_len (i32.const 9)) (return))) (if (i32.eq (local.get $s) (i32.const 404)) (then (global.set $ret_ptr (i32.const 0x1320)) (global.set $ret_len (i32.const 9)) (return))) (if (i32.eq (local.get $s) (i32.const 405)) (then (global.set $ret_ptr (i32.const 0x1330)) (global.set $ret_len (i32.const 18)) (return))) (if (i32.eq (local.get $s) (i32.const 408)) (then (global.set $ret_ptr (i32.const 0x1348)) (global.set $ret_len (i32.const 15)) (return))) (if (i32.eq (local.get $s) (i32.const 409)) (then (global.set $ret_ptr (i32.const 0x1358)) (global.set $ret_len (i32.const 8)) (return))) (if (i32.eq (local.get $s) (i32.const 410)) (then (global.set $ret_ptr (i32.const 0x1368)) (global.set $ret_len (i32.const 4)) (return))) (if (i32.eq (local.get $s) (i32.const 411)) (then (global.set $ret_ptr (i32.const 0x1370)) (global.set $ret_len (i32.const 15)) (return))) (if (i32.eq (local.get $s) (i32.const 413)) (then (global.set $ret_ptr (i32.const 0x1380)) (global.set $ret_len (i32.const 17)) (return))) (if (i32.eq (local.get $s) (i32.const 414)) (then (global.set $ret_ptr (i32.const 0x1398)) (global.set $ret_len (i32.const 12)) (return))) (if (i32.eq (local.get $s) (i32.const 415)) (then (global.set $ret_ptr (i32.const 0x13A8)) (global.set $ret_len (i32.const 22)) (return))) (if (i32.eq (local.get $s) (i32.const 416)) (then (global.set $ret_ptr (i32.const 0x13C0)) (global.set $ret_len (i32.const 21)) (return))) (if (i32.eq (local.get $s) (i32.const 418)) (then (global.set $ret_ptr (i32.const 0x13D8)) (global.set $ret_len (i32.const 12)) (return))) (if (i32.eq (local.get $s) (i32.const 422)) (then (global.set $ret_ptr (i32.const 0x13E8)) (global.set $ret_len (i32.const 21)) (return))) (if (i32.eq (local.get $s) (i32.const 429)) (then (global.set $ret_ptr (i32.const 0x1400)) (global.set $ret_len (i32.const 17)) (return))) (if (i32.eq (local.get $s) (i32.const 500)) (then (global.set $ret_ptr (i32.const 0x1418)) (global.set $ret_len (i32.const 21)) (return))) (if (i32.eq (local.get $s) (i32.const 501)) (then (global.set $ret_ptr (i32.const 0x1430)) (global.set $ret_len (i32.const 15)) (return))) (if (i32.eq (local.get $s) (i32.const 502)) (then (global.set $ret_ptr (i32.const 0x1440)) (global.set $ret_len (i32.const 11)) (return))) (if (i32.eq (local.get $s) (i32.const 503)) (then (global.set $ret_ptr (i32.const 0x1450)) (global.set $ret_len (i32.const 19)) (return))) (if (i32.eq (local.get $s) (i32.const 504)) (then (global.set $ret_ptr (i32.const 0x1468)) (global.set $ret_len (i32.const 15)) (return))) ) (func $http_err_name (param $ec i32) (global.set $ret_ptr (i32.const 0x2458)) (global.set $ret_len (i32.const 14)) (if (i32.eq (local.get $ec) (i32.const 0)) (then (global.set $ret_ptr (i32.const 0x2000)) (global.set $ret_len (i32.const 11)) (return))) (if (i32.eq (local.get $ec) (i32.const 1)) (then (global.set $ret_ptr (i32.const 0x2010)) (global.set $ret_len (i32.const 9)) (return))) (if (i32.eq (local.get $ec) (i32.const 2)) (then (global.set $ret_ptr (i32.const 0x2020)) (global.set $ret_len (i32.const 21)) (return))) (if (i32.eq (local.get $ec) (i32.const 3)) (then (global.set $ret_ptr (i32.const 0x2038)) (global.set $ret_len (i32.const 23)) (return))) (if (i32.eq (local.get $ec) (i32.const 4)) (then (global.set $ret_ptr (i32.const 0x2050)) (global.set $ret_len (i32.const 25)) (return))) (if (i32.eq (local.get $ec) (i32.const 5)) (then (global.set $ret_ptr (i32.const 0x2070)) (global.set $ret_len (i32.const 25)) (return))) (if (i32.eq (local.get $ec) (i32.const 6)) (then (global.set $ret_ptr (i32.const 0x2090)) (global.set $ret_len (i32.const 18)) (return))) (if (i32.eq (local.get $ec) (i32.const 7)) (then (global.set $ret_ptr (i32.const 0x20A8)) (global.set $ret_len (i32.const 21)) (return))) (if (i32.eq (local.get $ec) (i32.const 8)) (then (global.set $ret_ptr (i32.const 0x20C0)) (global.set $ret_len (i32.const 18)) (return))) (if (i32.eq (local.get $ec) (i32.const 9)) (then (global.set $ret_ptr (i32.const 0x20D8)) (global.set $ret_len (i32.const 23)) (return))) (if (i32.eq (local.get $ec) (i32.const 10)) (then (global.set $ret_ptr (i32.const 0x20F0)) (global.set $ret_len (i32.const 24)) (return))) (if (i32.eq (local.get $ec) (i32.const 11)) (then (global.set $ret_ptr (i32.const 0x2110)) (global.set $ret_len (i32.const 24)) (return))) (if (i32.eq (local.get $ec) (i32.const 12)) (then (global.set $ret_ptr (i32.const 0x2130)) (global.set $ret_len (i32.const 18)) (return))) (if (i32.eq (local.get $ec) (i32.const 13)) (then (global.set $ret_ptr (i32.const 0x2148)) (global.set $ret_len (i32.const 21)) (return))) (if (i32.eq (local.get $ec) (i32.const 14)) (then (global.set $ret_ptr (i32.const 0x2160)) (global.set $ret_len (i32.const 18)) (return))) (if (i32.eq (local.get $ec) (i32.const 15)) (then (global.set $ret_ptr (i32.const 0x2178)) (global.set $ret_len (i32.const 19)) (return))) (if (i32.eq (local.get $ec) (i32.const 16)) (then (global.set $ret_ptr (i32.const 0x2190)) (global.set $ret_len (i32.const 28)) (return))) (if (i32.eq (local.get $ec) (i32.const 17)) (then (global.set $ret_ptr (i32.const 0x21B0)) (global.set $ret_len (i32.const 28)) (return))) (if (i32.eq (local.get $ec) (i32.const 18)) (then (global.set $ret_ptr (i32.const 0x21D0)) (global.set $ret_len (i32.const 27)) (return))) (if (i32.eq (local.get $ec) (i32.const 19)) (then (global.set $ret_ptr (i32.const 0x21F0)) (global.set $ret_len (i32.const 24)) (return))) (if (i32.eq (local.get $ec) (i32.const 20)) (then (global.set $ret_ptr (i32.const 0x2210)) (global.set $ret_len (i32.const 25)) (return))) (if (i32.eq (local.get $ec) (i32.const 21)) (then (global.set $ret_ptr (i32.const 0x2230)) (global.set $ret_len (i32.const 37)) (return))) (if (i32.eq (local.get $ec) (i32.const 22)) (then (global.set $ret_ptr (i32.const 0x2258)) (global.set $ret_len (i32.const 29)) (return))) (if (i32.eq (local.get $ec) (i32.const 23)) (then (global.set $ret_ptr (i32.const 0x2278)) (global.set $ret_len (i32.const 38)) (return))) (if (i32.eq (local.get $ec) (i32.const 24)) (then (global.set $ret_ptr (i32.const 0x22A0)) (global.set $ret_len (i32.const 30)) (return))) (if (i32.eq (local.get $ec) (i32.const 25)) (then (global.set $ret_ptr (i32.const 0x22C0)) (global.set $ret_len (i32.const 24)) (return))) (if (i32.eq (local.get $ec) (i32.const 26)) (then (global.set $ret_ptr (i32.const 0x22E0)) (global.set $ret_len (i32.const 38)) (return))) (if (i32.eq (local.get $ec) (i32.const 27)) (then (global.set $ret_ptr (i32.const 0x2308)) (global.set $ret_len (i32.const 30)) (return))) (if (i32.eq (local.get $ec) (i32.const 28)) (then (global.set $ret_ptr (i32.const 0x2328)) (global.set $ret_len (i32.const 29)) (return))) (if (i32.eq (local.get $ec) (i32.const 29)) (then (global.set $ret_ptr (i32.const 0x2348)) (global.set $ret_len (i32.const 39)) (return))) (if (i32.eq (local.get $ec) (i32.const 30)) (then (global.set $ret_ptr (i32.const 0x2370)) (global.set $ret_len (i32.const 31)) (return))) (if (i32.eq (local.get $ec) (i32.const 31)) (then (global.set $ret_ptr (i32.const 0x2390)) (global.set $ret_len (i32.const 35)) (return))) (if (i32.eq (local.get $ec) (i32.const 32)) (then (global.set $ret_ptr (i32.const 0x23B8)) (global.set $ret_len (i32.const 34)) (return))) (if (i32.eq (local.get $ec) (i32.const 33)) (then (global.set $ret_ptr (i32.const 0x23E0)) (global.set $ret_len (i32.const 21)) (return))) (if (i32.eq (local.get $ec) (i32.const 34)) (then (global.set $ret_ptr (i32.const 0x23F8)) (global.set $ret_len (i32.const 19)) (return))) (if (i32.eq (local.get $ec) (i32.const 35)) (then (global.set $ret_ptr (i32.const 0x2410)) (global.set $ret_len (i32.const 19)) (return))) (if (i32.eq (local.get $ec) (i32.const 36)) (then (global.set $ret_ptr (i32.const 0x2428)) (global.set $ret_len (i32.const 22)) (return))) (if (i32.eq (local.get $ec) (i32.const 37)) (then (global.set $ret_ptr (i32.const 0x2440)) (global.set $ret_len (i32.const 19)) (return))) ) (func $print_http_error (param $ec i32) (if (i32.eqz (call $should_show_err)) (then (return))) (call $http_err_name (local.get $ec)) (call $write_err (i32.const 0x0D00) (i32.const 6)) ;; "curl: " (if (i32.or (i32.eq (local.get $ec) (i32.const 8)) (i32.or (i32.eq (local.get $ec) (i32.const 9)) (i32.or (i32.eq (local.get $ec) (i32.const 10)) (i32.eq (local.get $ec) (i32.const 33))))) (then (call $write_err (i32.const 0x0EA8) (i32.const 25)) (drop (call $stream_flush (global.get $g_stderr))) (return) ) ) (if (i32.or (i32.eq (local.get $ec) (i32.const 0)) (i32.eq (local.get $ec) (i32.const 1))) (then (call $write_err (i32.const 0x0EC8) (i32.const 28)) (call $write_err (global.get $url_auth_ptr) (global.get $url_auth_len)) (call $write_err (i32.const 0x0A00) (i32.const 1)) (drop (call $stream_flush (global.get $g_stderr))) (return) ) ) (if (i32.or (i32.and (i32.ge_u (local.get $ec) (i32.const 2)) (i32.le_u (local.get $ec) (i32.const 6))) (i32.or (i32.eq (local.get $ec) (i32.const 11)) (i32.eq (local.get $ec) (i32.const 15)))) (then (call $write_err (i32.const 0x0EE8) (i32.const 31)) (call $write_err (global.get $ret_ptr) (global.get $ret_len)) (call $write_err (i32.const 0x0A00) (i32.const 1)) (drop (call $stream_flush (global.get $g_stderr))) (return) ) ) (if (i32.and (i32.ge_u (local.get $ec) (i32.const 12)) (i32.le_u (local.get $ec) (i32.const 14))) (then (call $write_err (i32.const 0x0F08) (i32.const 24)) (call $write_err (global.get $ret_ptr) (global.get $ret_len)) (call $write_err (i32.const 0x0A00) (i32.const 1)) (drop (call $stream_flush (global.get $g_stderr))) (return) ) ) (if (i32.or (i32.eq (local.get $ec) (i32.const 7)) (i32.eq (local.get $ec) (i32.const 25))) (then (call $write_err (i32.const 0x0F20) (i32.const 51)) (drop (call $stream_flush (global.get $g_stderr))) (return) ) ) (if (i32.and (i32.ge_u (local.get $ec) (i32.const 18)) (i32.le_u (local.get $ec) (i32.const 20))) (then (call $write_err (i32.const 0x0F88) (i32.const 34)) (drop (call $stream_flush (global.get $g_stderr))) (return) ) ) (call $write_err (i32.const 0x0F58) (i32.const 42)) (call $write_err (global.get $ret_ptr) (global.get $ret_len)) (call $write_err (i32.const 0x0A00) (i32.const 1)) (drop (call $stream_flush (global.get $g_stderr))) ) ;; Write HTTP status line + header entries to given output stream (func $write_status_and_headers (param $stream i32) (param $status i32) (param $ent_p i32) (param $ent_cnt i32) (param $pfx_p i32) (param $pfx_l i32) (local $i i32) (local $item i32) (local $kp i32) (local $kl i32) (local $vp i32) (local $vl i32) (if (i32.gt_u (local.get $pfx_l) (i32.const 0)) (then (drop (call $stream_write_all (local.get $stream) (local.get $pfx_p) (local.get $pfx_l))) )) (drop (call $stream_write_all (local.get $stream) (i32.const 0x0BC0) (i32.const 9))) ;; "HTTP/1.1 " (call $write_u32_to (local.get $stream) (local.get $status)) (call $lookup_reason (local.get $status)) (if (i32.gt_u (global.get $ret_len) (i32.const 0)) (then (drop (call $stream_write_all (local.get $stream) (i32.const 0x0A0C) (i32.const 1))) (drop (call $stream_write_all (local.get $stream) (global.get $ret_ptr) (global.get $ret_len))) )) (drop (call $stream_write_all (local.get $stream) (i32.const 0x0A04) (i32.const 2))) ;; "\r\n" (loop $l (if (i32.lt_u (local.get $i) (local.get $ent_cnt)) (then (local.set $item (i32.add (local.get $ent_p) (i32.mul (local.get $i) (i32.const 16)))) (local.set $kp (i32.load (local.get $item))) (local.set $kl (i32.load (i32.add (local.get $item) (i32.const 4)))) (local.set $vp (i32.load (i32.add (local.get $item) (i32.const 8)))) (local.set $vl (i32.load (i32.add (local.get $item) (i32.const 12)))) (if (i32.gt_u (local.get $pfx_l) (i32.const 0)) (then (drop (call $stream_write_all (local.get $stream) (local.get $pfx_p) (local.get $pfx_l))) )) (drop (call $stream_write_all (local.get $stream) (local.get $kp) (local.get $kl))) (drop (call $stream_write_all (local.get $stream) (i32.const 0x0A08) (i32.const 2))) ;; ": " (drop (call $stream_write_all (local.get $stream) (local.get $vp) (local.get $vl))) (drop (call $stream_write_all (local.get $stream) (i32.const 0x0A04) (i32.const 2))) ;; "\r\n" (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l) )) ) (if (i32.gt_u (local.get $pfx_l) (i32.const 0)) (then (drop (call $stream_write_all (local.get $stream) (local.get $pfx_p) (local.get $pfx_l))) )) (drop (call $stream_write_all (local.get $stream) (i32.const 0x0A04) (i32.const 2))) ) ;; Append one header safely, ignoring forbidden/invalid-syntax with optional -v log (func $safe_append_hdr (param $hdrs i32) (param $kp i32) (param $kl i32) (param $vp i32) (param $vl i32) (local $err i32) (call $fields_append (local.get $hdrs) (local.get $kp) (local.get $kl) (local.get $vp) (local.get $vl) (i32.const 0x7040)) (if (i32.ne (i32.load8_u (i32.const 0x7040)) (i32.const 0)) (then (if (global.get $opt_verbose) (then (local.set $err (i32.load8_u (i32.const 0x7041))) (call $write_err (i32.const 0x0BE8) (i32.const 2)) (if (i32.eq (local.get $err) (i32.const 1)) (then (call $write_err (i32.const 0x1008) (i32.const 38))) (else (call $write_err (i32.const 0x1030) (i32.const 23))) ) (call $write_err (local.get $kp) (local.get $kl)) (call $write_err (i32.const 0x0A00) (i32.const 1)) )) )) ) (func $build_request_headers (param $hdrs i32) (local $i i32) (local $slot i32) (local $kp i32) (local $kl i32) (local $vp i32) (local $vl i32) (local $fl i32) (local $cred_p i32) (local $cred_l i32) (local $b64_l i32) (local $need_colon i32) (local $tmp i32) ;; 1. Default User-Agent (if (i32.eqz (call $cli_has_header_named (i32.const 0x0AE0) (i32.const 10))) (then (if (global.get $opt_ua_set) (then (if (i32.gt_u (global.get $opt_ua_len) (i32.const 0)) (then (call $safe_append_hdr (local.get $hdrs) (i32.const 0x0AE0) (i32.const 10) (global.get $opt_ua_ptr) (global.get $opt_ua_len)) )) ) (else (call $safe_append_hdr (local.get $hdrs) (i32.const 0x0AE0) (i32.const 10) (i32.const 0x0AF0) (i32.const 15)) ) ) )) ;; 2. Default Accept: */* (if (i32.eqz (call $cli_has_header_named (i32.const 0x0B08) (i32.const 6))) (then (call $safe_append_hdr (local.get $hdrs) (i32.const 0x0B08) (i32.const 6) (i32.const 0x0B10) (i32.const 3)) )) ;; 3. Authorization (Basic) from -u or URL userinfo, unless cross-origin redirect or -H overridden (if (i32.and (i32.eqz (global.get $cross_origin_redir)) (i32.eqz (call $cli_has_header_named (i32.const 0x0B18) (i32.const 13)))) (then (if (global.get $opt_user_set) (then (local.set $cred_p (global.get $opt_user_ptr)) (local.set $cred_l (global.get $opt_user_len))) (else (if (global.get $url_has_userinfo) (then (local.set $cred_p (global.get $url_userinfo_ptr)) (local.set $cred_l (global.get $url_userinfo_len)) )) ) ) (if (i32.ne (local.get $cred_p) (i32.const 0)) (then (if (i32.eq (call $find_byte (local.get $cred_p) (local.get $cred_l) (i32.const 58)) (i32.const -1)) (then (local.set $tmp (call $alloc (i32.add (local.get $cred_l) (i32.const 1)) (i32.const 1))) (memory.copy (local.get $tmp) (local.get $cred_p) (local.get $cred_l)) (i32.store8 (i32.add (local.get $tmp) (local.get $cred_l)) (i32.const 58)) (local.set $cred_p (local.get $tmp)) (local.set $cred_l (i32.add (local.get $cred_l) (i32.const 1))) )) (memory.copy (i32.const 0xC000) (i32.const 0x0B28) (i32.const 6)) ;; "Basic " (local.set $b64_l (call $base64_encode (local.get $cred_p) (local.get $cred_l) (i32.const 0xC006))) (call $safe_append_hdr (local.get $hdrs) (i32.const 0x0B18) (i32.const 13) (i32.const 0xC000) (i32.add (i32.const 6) (local.get $b64_l))) )) ) ) ;; 4. Referer (-e) (if (i32.and (global.get $opt_referer_set) (i32.eqz (call $cli_has_header_named (i32.const 0x0B30) (i32.const 7)))) (then (call $safe_append_hdr (local.get $hdrs) (i32.const 0x0B30) (i32.const 7) (global.get $opt_referer_ptr) (global.get $opt_referer_len)) ) ) ;; 5. Cookie (-b), unless cross-origin redirect (if (i32.and (i32.and (global.get $opt_cookie_set) (i32.eqz (global.get $cross_origin_redir))) (i32.eqz (call $cli_has_header_named (i32.const 0x0B38) (i32.const 6)))) (then (call $safe_append_hdr (local.get $hdrs) (i32.const 0x0B38) (i32.const 6) (global.get $opt_cookie_ptr) (global.get $opt_cookie_len)) ) ) ;; 6. Range (-r) -> "bytes=" (if (i32.and (global.get $opt_range_set) (i32.eqz (call $cli_has_header_named (i32.const 0x0B40) (i32.const 5)))) (then (local.set $tmp (call $alloc (i32.add (i32.const 6) (global.get $opt_range_len)) (i32.const 1))) (memory.copy (local.get $tmp) (i32.const 0x0B48) (i32.const 6)) (memory.copy (i32.add (local.get $tmp) (i32.const 6)) (global.get $opt_range_ptr) (global.get $opt_range_len)) (call $safe_append_hdr (local.get $hdrs) (i32.const 0x0B40) (i32.const 5) (local.get $tmp) (i32.add (i32.const 6) (global.get $opt_range_len))) ) ) ;; 7. Content-Type for form data (-d*) (if (i32.and (i32.and (global.get $req_has_body) (global.get $req_is_form)) (i32.eqz (call $cli_has_header_named (i32.const 0x0B50) (i32.const 12)))) (then (call $safe_append_hdr (local.get $hdrs) (i32.const 0x0B50) (i32.const 12) (i32.const 0x0B60) (i32.const 33)) ) ) ;; 8. Content-Length when body present (if (i32.and (global.get $req_has_body) (i32.eqz (call $cli_has_header_named (i32.const 0x0B88) (i32.const 14)))) (then (local.set $tmp (call $u64_to_dec (i64.extend_i32_u (global.get $req_body_len)) (i32.const 0x8400))) (call $safe_append_hdr (local.get $hdrs) (i32.const 0x0B88) (i32.const 14) (i32.const 0x8400) (local.get $tmp)) ) ) ;; 9. User -H headers in order (loop $hl (if (i32.lt_u (local.get $i) (global.get $cli_hdr_cnt)) (then (local.set $slot (i32.add (i32.const 0x7400) (i32.mul (local.get $i) (i32.const 20)))) (local.set $kp (i32.load (local.get $slot))) (local.set $kl (i32.load (i32.add (local.get $slot) (i32.const 4)))) (local.set $vp (i32.load (i32.add (local.get $slot) (i32.const 8)))) (local.set $vl (i32.load (i32.add (local.get $slot) (i32.const 12)))) (local.set $fl (i32.load (i32.add (local.get $slot) (i32.const 16)))) (if (i32.and (global.get $cross_origin_redir) (i32.or (call $memeq_ci (local.get $kp) (local.get $kl) (i32.const 0x0B18) (i32.const 13)) (call $memeq_ci (local.get $kp) (local.get $kl) (i32.const 0x0B38) (i32.const 6)))) (then ;; Strip Authorization/Cookie on cross-origin redirect ) (else (if (i32.eq (local.get $fl) (i32.const 1)) (then (call $fields_delete (local.get $hdrs) (local.get $kp) (local.get $kl) (i32.const 0x7040))) (else (call $safe_append_hdr (local.get $hdrs) (local.get $kp) (local.get $kl) (local.get $vp) (local.get $vl))) ) ) ) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $hl) )) ) ) ;; ========================================================================== ;; 4f. --write-out Formatter & HTTP Transfer Engine ;; ========================================================================== (func $write_out_format (local $p i32) (local $len i32) (local $i i32) (local $c i32) (local $end i32) (local $vp i32) (local $vl i32) (local $n i32) (if (i32.eqz (global.get $opt_write_out_set)) (then (return))) (local.set $p (global.get $opt_write_out_ptr)) (local.set $len (global.get $opt_write_out_len)) (loop $l (if (i32.lt_u (local.get $i) (local.get $len)) (then (local.set $c (i32.load8_u (i32.add (local.get $p) (local.get $i)))) (if (i32.eq (local.get $c) (i32.const 92)) ;; '\' (then (if (i32.lt_u (i32.add (local.get $i) (i32.const 1)) (local.get $len)) (then (local.set $c (i32.load8_u (i32.add (local.get $p) (i32.add (local.get $i) (i32.const 1))))) (if (i32.eq (local.get $c) (i32.const 110)) (then (i32.store8 (i32.const 0x8400) (i32.const 10)) (call $write_out (i32.const 0x8400) (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 2))) (br $l))) (if (i32.eq (local.get $c) (i32.const 114)) (then (i32.store8 (i32.const 0x8400) (i32.const 13)) (call $write_out (i32.const 0x8400) (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 2))) (br $l))) (if (i32.eq (local.get $c) (i32.const 116)) (then (i32.store8 (i32.const 0x8400) (i32.const 9)) (call $write_out (i32.const 0x8400) (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 2))) (br $l))) (if (i32.eq (local.get $c) (i32.const 92)) (then (i32.store8 (i32.const 0x8400) (i32.const 92)) (call $write_out (i32.const 0x8400) (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 2))) (br $l))) )) (call $write_out (i32.add (local.get $p) (local.get $i)) (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l) ) ) (if (i32.eq (local.get $c) (i32.const 37)) ;; '%' (then (if (i32.lt_u (i32.add (local.get $i) (i32.const 1)) (local.get $len)) (then (local.set $c (i32.load8_u (i32.add (local.get $p) (i32.add (local.get $i) (i32.const 1))))) (if (i32.eq (local.get $c) (i32.const 37)) (then ;; "%%" (call $write_out (i32.add (local.get $p) (local.get $i)) (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 2))) (br $l) )) (if (i32.eq (local.get $c) (i32.const 123)) (then ;; "%{" (local.set $end (call $find_byte (i32.add (local.get $p) (i32.add (local.get $i) (i32.const 2))) (i32.sub (local.get $len) (i32.add (local.get $i) (i32.const 2))) (i32.const 125))) ;; '}' (if (i32.ne (local.get $end) (i32.const -1)) (then (local.set $vp (i32.add (local.get $p) (i32.add (local.get $i) (i32.const 2)))) (local.set $vl (local.get $end)) (if (i32.or (call $memeq (local.get $vp) (local.get $vl) (i32.const 0x1800) (i32.const 9)) (call $memeq (local.get $vp) (local.get $vl) (i32.const 0x180C) (i32.const 13))) (then (local.set $n (call $u32_to_dec_pad (global.get $stat_status) (i32.const 0x8400) (i32.const 3))) (call $write_out (i32.const 0x8400) (local.get $n)) ) (else (if (call $memeq (local.get $vp) (local.get $vl) (i32.const 0x181C) (i32.const 13)) (then (call $write_u64_to (global.get $g_stdout) (global.get $stat_size_down))) (else (if (call $memeq (local.get $vp) (local.get $vl) (i32.const 0x182C) (i32.const 11)) (then (call $write_u64_to (global.get $g_stdout) (global.get $stat_size_up))) (else (if (call $memeq (local.get $vp) (local.get $vl) (i32.const 0x1838) (i32.const 10)) (then (local.set $n (call $format_time_total (global.get $stat_elapsed_ns) (i32.const 0x8400))) (call $write_out (i32.const 0x8400) (local.get $n)) ) (else (if (call $memeq (local.get $vp) (local.get $vl) (i32.const 0x1844) (i32.const 13)) (then (call $write_out (global.get $url_eff_ptr) (global.get $url_eff_len))) (else (if (call $memeq (local.get $vp) (local.get $vl) (i32.const 0x1854) (i32.const 12)) (then (call $write_out (global.get $stat_ct_ptr) (global.get $stat_ct_len))) (else (if (call $memeq (local.get $vp) (local.get $vl) (i32.const 0x1864) (i32.const 12)) (then (call $write_out (global.get $stat_redir_ptr) (global.get $stat_redir_len))) (else (if (call $memeq (local.get $vp) (local.get $vl) (i32.const 0x1874) (i32.const 13)) (then (call $write_u32_to (global.get $g_stdout) (global.get $stat_num_redirs))) (else (if (call $memeq (local.get $vp) (local.get $vl) (i32.const 0x1884) (i32.const 6)) (then (if (i32.eq (global.get $url_scheme) (i32.const 1)) (then (call $write_out (i32.const 0x0A58) (i32.const 5))) (else (call $write_out (i32.const 0x0A50) (i32.const 4))) ) ) (else (if (call $memeq (local.get $vp) (local.get $vl) (i32.const 0x188C) (i32.const 6)) (then (call $write_out (global.get $req_method_ptr) (global.get $req_method_len))) (else (call $write_out (i32.add (local.get $p) (local.get $i)) (i32.add (local.get $end) (i32.const 3))) )))))))))))))))))))) (local.set $i (i32.add (local.get $i) (i32.add (local.get $end) (i32.const 3)))) (br $l) )) )) )) ) ) (call $write_out (i32.add (local.get $p) (local.get $i)) (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $l) )) ) (drop (call $stream_flush (global.get $g_stdout))) ) (func $ensure_main_output_open (result i32) (if (global.get $cur_out_opened) (then (return (i32.const 0)))) (if (global.get $eff_out_is_file) (then (if (call $open_output_sink (global.get $eff_out_path_ptr) (global.get $eff_out_path_len)) (then (return (i32.const 1)))) (global.set $cur_out_mode (global.get $ret_ptr)) (global.set $cur_out_fd (global.get $ret_len)) (global.set $cur_out_stream (global.get $ret_val3)) ) (else (global.set $cur_out_mode (i32.const 0)) (global.set $cur_out_fd (i32.const 0)) (global.set $cur_out_stream (global.get $g_stdout)) ) ) (global.set $cur_out_opened (i32.const 1)) (i32.const 0) ) (func $ensure_dump_open (result i32) (if (i32.eqz (global.get $opt_dump_hdr_set)) (then (return (i32.const 0)))) (if (global.get $cur_dump_opened) (then (return (i32.const 0)))) (if (call $open_output_sink (global.get $opt_dump_hdr_ptr) (global.get $opt_dump_hdr_len)) (then (return (i32.const 1)))) (global.set $cur_dump_mode (i32.add (global.get $ret_ptr) (i32.const 1))) ;; 1=stdout,2=file,3=null (global.set $cur_dump_fd (global.get $ret_len)) (global.set $cur_dump_stream (global.get $ret_val3)) (global.set $cur_dump_opened (i32.const 1)) (i32.const 0) ) (func $close_output_sinks (if (global.get $cur_out_opened) (then (if (i32.eq (global.get $cur_out_mode) (i32.const 0)) (then (drop (call $stream_flush (global.get $cur_out_stream))) )) (if (i32.eq (global.get $cur_out_mode) (i32.const 1)) (then (drop (call $stream_flush (global.get $cur_out_stream))) (call $drop_output_stream (global.get $cur_out_stream)) (call $drop_descriptor (global.get $cur_out_fd)) )) (global.set $cur_out_opened (i32.const 0)) )) (if (global.get $cur_dump_opened) (then (if (i32.eq (global.get $cur_dump_mode) (i32.const 1)) (then (drop (call $stream_flush (global.get $cur_dump_stream))) )) (if (i32.eq (global.get $cur_dump_mode) (i32.const 2)) (then (drop (call $stream_flush (global.get $cur_dump_stream))) (call $drop_output_stream (global.get $cur_dump_stream)) (call $drop_descriptor (global.get $cur_dump_fd)) )) (global.set $cur_dump_opened (i32.const 0)) )) ) ;; Apply method on outgoing-request (func $apply_req_method (param $req i32) (param $mp i32) (param $ml i32) (result i32) (local $tag i32) (local.set $tag (i32.const 9)) (if (call $memeq (local.get $mp) (local.get $ml) (i32.const 0x0A90) (i32.const 3)) (then (local.set $tag (i32.const 0)))) (if (call $memeq (local.get $mp) (local.get $ml) (i32.const 0x0A98) (i32.const 4)) (then (local.set $tag (i32.const 1)))) (if (call $memeq (local.get $mp) (local.get $ml) (i32.const 0x0AA0) (i32.const 4)) (then (local.set $tag (i32.const 2)))) (if (call $memeq (local.get $mp) (local.get $ml) (i32.const 0x0AA8) (i32.const 3)) (then (local.set $tag (i32.const 3)))) (if (call $memeq (local.get $mp) (local.get $ml) (i32.const 0x0AB0) (i32.const 6)) (then (local.set $tag (i32.const 4)))) (if (call $memeq (local.get $mp) (local.get $ml) (i32.const 0x0AB8) (i32.const 7)) (then (local.set $tag (i32.const 5)))) (if (call $memeq (local.get $mp) (local.get $ml) (i32.const 0x0AC0) (i32.const 7)) (then (local.set $tag (i32.const 6)))) (if (call $memeq (local.get $mp) (local.get $ml) (i32.const 0x0AC8) (i32.const 5)) (then (local.set $tag (i32.const 7)))) (if (call $memeq (local.get $mp) (local.get $ml) (i32.const 0x0AD0) (i32.const 5)) (then (local.set $tag (i32.const 8)))) (call $req_set_method (local.get $req) (local.get $tag) (local.get $mp) (local.get $ml)) ) ;; Fetch a single URL (with redirect support). Returns 0 ok, 1 error. (func $fetch_url (param $uptr i32) (param $ulen i32) (result i32) (local $hdrs i32) (local $req i32) (local $ob i32) (local $os i32) (local $opts i32) (local $has_opts i32) (local $fir i32) (local $poll i32) (local $resp i32) (local $rhdrs i32) (local $ibody i32) (local $is i32) (local $ent_p i32) (local $ent_cnt i32) (local $i i32) (local $item i32) (local $kp i32) (local $kl i32) (local $vp i32) (local $vl i32) (local $status i32) (local $is_redir i32) (local $ec i32) (local $now i64) (local $rem i64) (local $ct_ns i64) (local $mark i32) (local $cptr i32) (local $clen i32) ;; Reset per-URL stats (global.set $stat_status (i32.const 0)) (global.set $stat_size_down (i64.const 0)) (global.set $stat_size_up (i64.const 0)) (global.set $stat_num_redirs (i32.const 0)) (global.set $stat_ct_ptr (i32.const 0)) (global.set $stat_ct_len (i32.const 0)) (global.set $stat_redir_ptr (i32.const 0)) (global.set $stat_redir_len (i32.const 0)) (global.set $cross_origin_redir (i32.const 0)) (global.set $cur_out_opened (i32.const 0)) (global.set $cur_dump_opened (i32.const 0)) (global.set $stat_start_ns (call $clock_now)) (if (global.get $opt_max_time_set) (then (global.set $stat_deadline_ns (i64.add (global.get $stat_start_ns) (global.get $opt_max_time_ns))) )) ;; Parse initial URL (if (call $parse_url (local.get $uptr) (local.get $ulen)) (then (if (call $should_show_err) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0D80) (i32.const 52)) (drop (call $stream_flush (global.get $g_stderr))) )) (return (i32.const 1)) )) ;; -G / --get (if (i32.and (global.get $opt_get) (global.get $has_data)) (then (call $append_query_to_url (global.get $data_buf_ptr) (global.get $data_buf_len)) )) ;; Body setup: -T or -d (without -G) (if (global.get $opt_upload_set) (then (if (call $read_file_or_stdin (global.get $opt_upload_ptr) (global.get $opt_upload_len) (i32.const 0)) (then (return (i32.const 1)) )) (global.set $req_has_body (i32.const 1)) (global.set $req_body_ptr (global.get $ret_ptr)) (global.set $req_body_len (global.get $ret_len)) (global.set $req_is_form (i32.const 0)) (call $maybe_append_upload_basename (global.get $opt_upload_ptr) (global.get $opt_upload_len)) ) (else (if (i32.and (global.get $has_data) (i32.eqz (global.get $opt_get))) (then (global.set $req_has_body (i32.const 1)) (global.set $req_body_ptr (global.get $data_buf_ptr)) (global.set $req_body_len (global.get $data_buf_len)) (global.set $req_is_form (i32.const 1)) ) (else (global.set $req_has_body (i32.const 0)) (global.set $req_body_ptr (i32.const 0)) (global.set $req_body_len (i32.const 0)) (global.set $req_is_form (i32.const 0)) ) ) ) ) ;; Method setup: -X > -I > -T > -d > GET (if (i32.ne (global.get $opt_method_ptr) (i32.const 0)) (then (global.set $req_method_ptr (global.get $opt_method_ptr)) (global.set $req_method_len (global.get $opt_method_len))) (else (if (global.get $opt_head) (then (global.set $req_method_ptr (i32.const 0x0A98)) (global.set $req_method_len (i32.const 4))) (else (if (global.get $opt_upload_set) (then (global.set $req_method_ptr (i32.const 0x0AA8)) (global.set $req_method_len (i32.const 3))) (else (if (i32.and (global.get $has_data) (i32.eqz (global.get $opt_get))) (then (global.set $req_method_ptr (i32.const 0x0AA0)) (global.set $req_method_len (i32.const 4))) (else (global.set $req_method_ptr (i32.const 0x0A90)) (global.set $req_method_len (i32.const 3))) ) ) ) ) ) ) ) ;; Determine effective output target (-O / -o / stdout) (if (global.get $opt_remote_name) (then (if (call $extract_remote_name) (then (if (call $should_show_err) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0E80) (i32.const 37)) (drop (call $stream_flush (global.get $g_stderr))) )) (return (i32.const 1)) )) (global.set $eff_out_is_file (i32.const 1)) (global.set $eff_out_path_ptr (global.get $ret_ptr)) (global.set $eff_out_path_len (global.get $ret_len)) ) (else (if (global.get $opt_output_set) (then (if (call $memeq (global.get $opt_output_ptr) (global.get $opt_output_len) (i32.const 0x0A24) (i32.const 1)) (then (global.set $eff_out_is_file (i32.const 0))) (else (global.set $eff_out_is_file (i32.const 1)) (global.set $eff_out_path_ptr (global.get $opt_output_ptr)) (global.set $eff_out_path_len (global.get $opt_output_len)) ) ) ) (else (global.set $eff_out_is_file (i32.const 0))) ) ) ) ;; ==================== Hop / Redirect Loop ==================== (loop $hop_loop (if (global.get $opt_max_time_set) (then (local.set $now (call $clock_now)) (if (i64.ge_u (local.get $now) (global.get $stat_deadline_ns)) (then (if (call $should_show_err) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0EA8) (i32.const 25)) (drop (call $stream_flush (global.get $g_stderr))) )) (call $close_output_sinks) (global.set $stat_elapsed_ns (i64.sub (call $clock_now) (global.get $stat_start_ns))) (call $write_out_format) (return (i32.const 1)) )) )) ;; Build headers & outgoing-request (local.set $hdrs (call $new_fields)) (call $build_request_headers (local.get $hdrs)) ;; Verbose request trace (if (global.get $opt_verbose) (then (call $write_err (i32.const 0x0BE8) (i32.const 2)) (call $write_err (i32.const 0x0FF8) (i32.const 14)) (call $write_err (global.get $url_auth_ptr) (global.get $url_auth_len)) (call $write_err (i32.const 0x0A00) (i32.const 1)) (call $write_err (i32.const 0x0BE0) (i32.const 2)) (call $write_err (global.get $req_method_ptr) (global.get $req_method_len)) (call $write_err (i32.const 0x0A0C) (i32.const 1)) (call $write_err (global.get $url_path_ptr) (global.get $url_path_len)) (call $write_err (i32.const 0x0BD0) (i32.const 10)) (call $write_err (i32.const 0x0BE0) (i32.const 2)) (call $write_err (i32.const 0x0B98) (i32.const 4)) (call $write_err (i32.const 0x0A08) (i32.const 2)) (call $write_err (global.get $url_auth_ptr) (global.get $url_auth_len)) (call $write_err (i32.const 0x0A00) (i32.const 1)) (call $fields_entries (local.get $hdrs) (i32.const 0x7040)) (local.set $ent_p (i32.load (i32.const 0x7040))) (local.set $ent_cnt (i32.load (i32.const 0x7044))) (local.set $i (i32.const 0)) (loop $vh (if (i32.lt_u (local.get $i) (local.get $ent_cnt)) (then (local.set $item (i32.add (local.get $ent_p) (i32.mul (local.get $i) (i32.const 16)))) (call $write_err (i32.const 0x0BE0) (i32.const 2)) (call $write_err (i32.load (local.get $item)) (i32.load (i32.add (local.get $item) (i32.const 4)))) (call $write_err (i32.const 0x0A08) (i32.const 2)) (call $write_err (i32.load (i32.add (local.get $item) (i32.const 8))) (i32.load (i32.add (local.get $item) (i32.const 12)))) (call $write_err (i32.const 0x0A00) (i32.const 1)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $vh) )) ) (call $write_err (i32.const 0x0BEC) (i32.const 2)) (drop (call $stream_flush (global.get $g_stderr))) )) ;; new_outgoing_request consumes $hdrs (local.set $req (call $new_outgoing_request (local.get $hdrs))) (if (i32.or (call $apply_req_method (local.get $req) (global.get $req_method_ptr) (global.get $req_method_len)) (i32.or (call $req_set_scheme (local.get $req) (i32.const 1) (global.get $url_scheme) (i32.const 0) (i32.const 0)) (i32.or (call $req_set_authority (local.get $req) (i32.const 1) (global.get $url_auth_ptr) (global.get $url_auth_len)) (call $req_set_path (local.get $req) (i32.const 1) (global.get $url_path_ptr) (global.get $url_path_len))))) (then (call $drop_outgoing_request (local.get $req)) (if (call $should_show_err) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0F88) (i32.const 34)) (drop (call $stream_flush (global.get $g_stderr))) )) (call $close_output_sinks) (global.set $stat_elapsed_ns (i64.sub (call $clock_now) (global.get $stat_start_ns))) (call $write_out_format) (return (i32.const 1)) ) ) ;; Obtain outgoing-body + stream BEFORE handle() if body present (local.set $ob (i32.const 0)) (local.set $os (i32.const 0)) (if (global.get $req_has_body) (then (call $outgoing_request_body (local.get $req) (i32.const 0x7040)) (if (i32.ne (i32.load8_u (i32.const 0x7040)) (i32.const 0)) (then (call $drop_outgoing_request (local.get $req)) (call $close_output_sinks) (return (i32.const 1)) )) (local.set $ob (i32.load (i32.const 0x7044))) (call $outgoing_body_write (local.get $ob) (i32.const 0x7040)) (if (i32.ne (i32.load8_u (i32.const 0x7040)) (i32.const 0)) (then (call $drop_outgoing_body (local.get $ob)) (call $drop_outgoing_request (local.get $req)) (call $close_output_sinks) (return (i32.const 1)) )) (local.set $os (i32.load (i32.const 0x7044))) )) ;; Build request-options if timeout configured (local.set $has_opts (i32.const 0)) (local.set $opts (i32.const 0)) (if (i32.or (global.get $opt_connect_timeout_set) (global.get $opt_max_time_set)) (then (local.set $has_opts (i32.const 1)) (local.set $opts (call $new_request_options)) (if (global.get $opt_max_time_set) (then (local.set $rem (i64.sub (global.get $stat_deadline_ns) (call $clock_now))) (if (i64.le_s (local.get $rem) (i64.const 0)) (then (local.set $rem (i64.const 1)))) (local.set $ct_ns (local.get $rem)) (if (i32.and (global.get $opt_connect_timeout_set) (i64.lt_u (global.get $opt_connect_timeout_ns) (local.get $rem))) (then (local.set $ct_ns (global.get $opt_connect_timeout_ns)) )) (drop (call $ro_set_connect_timeout (local.get $opts) (i32.const 1) (local.get $ct_ns))) (drop (call $ro_set_first_byte_timeout (local.get $opts) (i32.const 1) (local.get $rem))) (drop (call $ro_set_between_bytes_timeout (local.get $opts) (i32.const 1) (local.get $rem))) ) (else (drop (call $ro_set_connect_timeout (local.get $opts) (i32.const 1) (global.get $opt_connect_timeout_ns))) ) ) )) ;; Call handle(req, opts) -> consumes req and opts! (call $http_handle (local.get $req) (local.get $has_opts) (local.get $opts) (i32.const 0x7040)) (if (i32.ne (i32.load8_u (i32.const 0x7040)) (i32.const 0)) (then (local.set $ec (i32.load8_u (i32.const 0x7048))) (if (global.get $req_has_body) (then (call $drop_output_stream (local.get $os)) (call $drop_outgoing_body (local.get $ob)) )) (call $print_http_error (local.get $ec)) (call $close_output_sinks) (global.set $stat_elapsed_ns (i64.sub (call $clock_now) (global.get $stat_start_ns))) (call $write_out_format) (return (i32.const 1)) )) (local.set $fir (i32.load (i32.const 0x7048))) ;; Stream request body AFTER handle(), then drop stream, then finish() (if (global.get $req_has_body) (then (if (i32.gt_u (global.get $req_body_len) (i32.const 0)) (then (if (call $stream_write_all (local.get $os) (global.get $req_body_ptr) (global.get $req_body_len)) (then (call $drop_output_stream (local.get $os)) (call $drop_outgoing_body (local.get $ob)) (call $drop_fir (local.get $fir)) (call $close_output_sinks) (if (call $should_show_err) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x10A8) (i32.const 17)) (drop (call $stream_flush (global.get $g_stderr))) )) (return (i32.const 1)) )) (drop (call $stream_flush (local.get $os))) )) (call $drop_output_stream (local.get $os)) (call $outgoing_body_finish (local.get $ob) (i32.const 0) (i32.const 0) (i32.const 0x7040)) (if (i32.ne (i32.load8_u (i32.const 0x7040)) (i32.const 0)) (then (local.set $ec (i32.load8_u (i32.const 0x7048))) (call $drop_fir (local.get $fir)) (call $print_http_error (local.get $ec)) (call $close_output_sinks) (global.set $stat_elapsed_ns (i64.sub (call $clock_now) (global.get $stat_start_ns))) (call $write_out_format) (return (i32.const 1)) )) (global.set $stat_size_up (i64.add (global.get $stat_size_up) (i64.extend_i32_u (global.get $req_body_len)))) (if (global.get $opt_verbose) (then (call $write_err (i32.const 0x0BE8) (i32.const 2)) (call $write_err (i32.const 0x10C0) (i32.const 17)) (call $write_u32_to (global.get $g_stderr) (global.get $req_body_len)) (call $write_err (i32.const 0x10D8) (i32.const 7)) (drop (call $stream_flush (global.get $g_stderr))) )) )) ;; Await future-incoming-response (local.set $resp (i32.const 0)) (loop $wait_fir (call $fir_get (local.get $fir) (i32.const 0x7040)) (if (i32.eq (i32.load8_u (i32.const 0x7040)) (i32.const 0)) (then (local.set $poll (call $fir_subscribe (local.get $fir))) (call $poll_block (local.get $poll)) (call $drop_pollable (local.get $poll)) (br $wait_fir) ) ) (if (i32.ne (i32.load8_u (i32.const 0x7048)) (i32.const 0)) (then (call $drop_fir (local.get $fir)) (call $close_output_sinks) (return (i32.const 1)) )) (if (i32.ne (i32.load8_u (i32.const 0x7050)) (i32.const 0)) (then (local.set $ec (i32.load8_u (i32.const 0x7058))) (call $drop_fir (local.get $fir)) (call $print_http_error (local.get $ec)) (call $close_output_sinks) (global.set $stat_elapsed_ns (i64.sub (call $clock_now) (global.get $stat_start_ns))) (call $write_out_format) (return (i32.const 1)) )) (local.set $resp (i32.load (i32.const 0x7058))) ) ;; Extract status & headers (local.set $status (call $resp_status (local.get $resp))) (global.set $stat_status (local.get $status)) (local.set $rhdrs (call $resp_headers (local.get $resp))) (call $fields_entries (local.get $rhdrs) (i32.const 0x7040)) (local.set $ent_p (i32.load (i32.const 0x7040))) (local.set $ent_cnt (i32.load (i32.const 0x7044))) (global.set $hop_loc_ptr (i32.const 0)) (global.set $hop_loc_len (i32.const 0)) (global.set $stat_ct_ptr (i32.const 0)) (global.set $stat_ct_len (i32.const 0)) (local.set $i (i32.const 0)) (loop $scan_rh (if (i32.lt_u (local.get $i) (local.get $ent_cnt)) (then (local.set $item (i32.add (local.get $ent_p) (i32.mul (local.get $i) (i32.const 16)))) (local.set $kp (i32.load (local.get $item))) (local.set $kl (i32.load (i32.add (local.get $item) (i32.const 4)))) (local.set $vp (i32.load (i32.add (local.get $item) (i32.const 8)))) (local.set $vl (i32.load (i32.add (local.get $item) (i32.const 12)))) (if (call $memeq_ci (local.get $kp) (local.get $kl) (i32.const 0x0BA0) (i32.const 8)) (then ;; "location" (global.set $hop_loc_ptr (local.get $vp)) (global.set $hop_loc_len (local.get $vl)) )) (if (call $memeq_ci (local.get $kp) (local.get $kl) (i32.const 0x0BB0) (i32.const 12)) (then ;; "content-type" (global.set $stat_ct_ptr (local.get $vp)) (global.set $stat_ct_len (local.get $vl)) )) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $scan_rh) )) ) (if (global.get $opt_verbose) (then (call $write_status_and_headers (global.get $g_stderr) (local.get $status) (local.get $ent_p) (local.get $ent_cnt) (i32.const 0x0BE4) (i32.const 2)) (drop (call $stream_flush (global.get $g_stderr))) )) ;; Redirect check (local.set $is_redir (i32.and (i32.and (global.get $opt_location) (i32.ne (global.get $hop_loc_len) (i32.const 0))) (i32.or (i32.or (i32.eq (local.get $status) (i32.const 301)) (i32.eq (local.get $status) (i32.const 302))) (i32.or (i32.eq (local.get $status) (i32.const 303)) (i32.or (i32.eq (local.get $status) (i32.const 307)) (i32.eq (local.get $status) (i32.const 308))))) ) ) (if (local.get $is_redir) (then ;; Emit hop headers to -D and -i/-I if enabled (if (global.get $opt_dump_hdr_set) (then (if (call $ensure_dump_open) (then (call $drop_fields (local.get $rhdrs)) (call $drop_incoming_response (local.get $resp)) (call $drop_fir (local.get $fir)) (call $close_output_sinks) (return (i32.const 1)) )) (if (i32.ne (global.get $cur_dump_mode) (i32.const 3)) (then (call $write_status_and_headers (global.get $cur_dump_stream) (local.get $status) (local.get $ent_p) (local.get $ent_cnt) (i32.const 0) (i32.const 0)) )) )) (if (i32.or (global.get $opt_include) (global.get $opt_head)) (then (if (call $ensure_main_output_open) (then (call $drop_fields (local.get $rhdrs)) (call $drop_incoming_response (local.get $resp)) (call $drop_fir (local.get $fir)) (call $close_output_sinks) (return (i32.const 1)) )) (if (i32.ne (global.get $cur_out_mode) (i32.const 2)) (then (call $write_status_and_headers (global.get $cur_out_stream) (local.get $status) (local.get $ent_p) (local.get $ent_cnt) (i32.const 0) (i32.const 0)) )) )) (call $drop_fields (local.get $rhdrs)) (call $drop_incoming_response (local.get $resp)) (call $drop_fir (local.get $fir)) ;; Check max-redirs (if (i32.and (i32.ge_s (global.get $opt_max_redirs) (i32.const 0)) (i32.ge_s (global.get $stat_num_redirs) (global.get $opt_max_redirs))) (then (global.set $stat_redir_ptr (global.get $hop_loc_ptr)) (global.set $stat_redir_len (global.get $hop_loc_len)) (if (call $should_show_err) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0DE0) (i32.const 14)) (call $write_u32_to (global.get $g_stderr) (global.get $opt_max_redirs)) (call $write_err (i32.const 0x0DF0) (i32.const 21)) (drop (call $stream_flush (global.get $g_stderr))) )) (call $close_output_sinks) (global.set $stat_elapsed_ns (i64.sub (call $clock_now) (global.get $stat_start_ns))) (call $write_out_format) (return (i32.const 1)) ) ) ;; Resolve location (if (call $resolve_redirect (global.get $hop_loc_ptr) (global.get $hop_loc_len)) (then (call $close_output_sinks) (return (i32.const 1)) )) (global.set $stat_num_redirs (i32.add (global.get $stat_num_redirs) (i32.const 1))) ;; RFC 9110 method/body adjustments (303 unless GET/HEAD -> GET; 301/302 if POST -> GET; 307/308 preserve) (if (i32.eq (local.get $status) (i32.const 303)) (then (if (i32.and (i32.eqz (call $memeq (global.get $req_method_ptr) (global.get $req_method_len) (i32.const 0x0A90) (i32.const 3))) (i32.eqz (call $memeq (global.get $req_method_ptr) (global.get $req_method_len) (i32.const 0x0A98) (i32.const 4)))) (then (global.set $req_method_ptr (i32.const 0x0A90)) (global.set $req_method_len (i32.const 3)) (global.set $req_has_body (i32.const 0)) (global.set $req_body_len (i32.const 0)) (global.set $req_is_form (i32.const 0)) ) ) ) (else (if (i32.and (i32.or (i32.eq (local.get $status) (i32.const 301)) (i32.eq (local.get $status) (i32.const 302))) (call $memeq (global.get $req_method_ptr) (global.get $req_method_len) (i32.const 0x0AA0) (i32.const 4))) (then (global.set $req_method_ptr (i32.const 0x0A90)) (global.set $req_method_len (i32.const 3)) (global.set $req_has_body (i32.const 0)) (global.set $req_body_len (i32.const 0)) (global.set $req_is_form (i32.const 0)) ) ) ) ) (if (global.get $opt_verbose) (then (call $write_err (i32.const 0x0BE8) (i32.const 2)) (call $write_err (i32.const 0x0FB0) (i32.const 15)) (call $write_err (global.get $url_eff_ptr) (global.get $url_eff_len)) (call $write_err (i32.const 0x0A00) (i32.const 1)) (if (global.get $cross_origin_redir) (then (call $write_err (i32.const 0x0BE8) (i32.const 2)) (call $write_err (i32.const 0x0FC0) (i32.const 54)) )) (drop (call $stream_flush (global.get $g_stderr))) )) (br $hop_loop) )) ;; Not following redirect: populate %{redirect_url} if 3xx + Location (if (i32.and (i32.ne (global.get $hop_loc_len) (i32.const 0)) (i32.and (i32.ge_u (local.get $status) (i32.const 300)) (i32.lt_u (local.get $status) (i32.const 400)))) (then (global.set $stat_redir_ptr (global.get $hop_loc_ptr)) (global.set $stat_redir_len (global.get $hop_loc_len)) ) ) ;; Check -f / --fail on status >= 400 BEFORE writing any output or opening -o file (if (i32.and (global.get $opt_fail) (i32.ge_u (local.get $status) (i32.const 400))) (then (call $drop_fields (local.get $rhdrs)) (call $drop_incoming_response (local.get $resp)) (call $drop_fir (local.get $fir)) (call $close_output_sinks) (if (call $should_show_err) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0DB8) (i32.const 39)) (call $write_u32_to (global.get $g_stderr) (local.get $status)) (call $write_err (i32.const 0x0A00) (i32.const 1)) (drop (call $stream_flush (global.get $g_stderr))) )) (global.set $stat_elapsed_ns (i64.sub (call $clock_now) (global.get $stat_start_ns))) (call $write_out_format) (return (i32.const 1)) )) ;; Write -D headers (if (global.get $opt_dump_hdr_set) (then (if (call $ensure_dump_open) (then (call $drop_fields (local.get $rhdrs)) (call $drop_incoming_response (local.get $resp)) (call $drop_fir (local.get $fir)) (call $close_output_sinks) (return (i32.const 1)) )) (if (i32.ne (global.get $cur_dump_mode) (i32.const 3)) (then (call $write_status_and_headers (global.get $cur_dump_stream) (local.get $status) (local.get $ent_p) (local.get $ent_cnt) (i32.const 0) (i32.const 0)) )) )) ;; Open main output sink (creates -o/-O file even if empty 200) (if (call $ensure_main_output_open) (then (call $drop_fields (local.get $rhdrs)) (call $drop_incoming_response (local.get $resp)) (call $drop_fir (local.get $fir)) (call $close_output_sinks) (return (i32.const 1)) )) ;; Write -i / -I headers to main output (if (i32.or (global.get $opt_include) (global.get $opt_head)) (then (if (i32.ne (global.get $cur_out_mode) (i32.const 2)) (then (call $write_status_and_headers (global.get $cur_out_stream) (local.get $status) (local.get $ent_p) (local.get $ent_cnt) (i32.const 0) (i32.const 0)) )) )) (call $drop_fields (local.get $rhdrs)) ;; Consume and stream response body unless HEAD or -I (if (i32.and (i32.eqz (global.get $opt_head)) (i32.eqz (call $memeq (global.get $req_method_ptr) (global.get $req_method_len) (i32.const 0x0A98) (i32.const 4)))) (then (call $resp_consume (local.get $resp) (i32.const 0x7040)) (if (i32.eq (i32.load8_u (i32.const 0x7040)) (i32.const 0)) (then (local.set $ibody (i32.load (i32.const 0x7044))) (call $body_stream (local.get $ibody) (i32.const 0x7040)) (if (i32.eq (i32.load8_u (i32.const 0x7040)) (i32.const 0)) (then (local.set $is (i32.load (i32.const 0x7044))) (local.set $mark (call $heap_mark)) (loop $body_loop (if (global.get $opt_max_time_set) (then (if (i64.ge_u (call $clock_now) (global.get $stat_deadline_ns)) (then (call $heap_reset (local.get $mark)) (call $drop_input_stream (local.get $is)) (call $drop_incoming_body (local.get $ibody)) (call $drop_incoming_response (local.get $resp)) (call $drop_fir (local.get $fir)) (call $close_output_sinks) (if (call $should_show_err) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0EA8) (i32.const 25)) (drop (call $stream_flush (global.get $g_stderr))) )) (global.set $stat_elapsed_ns (i64.sub (call $clock_now) (global.get $stat_start_ns))) (call $write_out_format) (return (i32.const 1)) )) )) (call $in_blocking_read (local.get $is) (i64.const 65536) (i32.const 0x7000)) (if (i32.eq (i32.load8_u (i32.const 0x7000)) (i32.const 0)) (then (local.set $cptr (i32.load (i32.const 0x7004))) (local.set $clen (i32.load (i32.const 0x7008))) (if (i32.gt_u (local.get $clen) (i32.const 0)) (then (global.set $stat_size_down (i64.add (global.get $stat_size_down) (i64.extend_i32_u (local.get $clen)))) (if (i32.ne (global.get $cur_out_mode) (i32.const 2)) (then (if (call $stream_write_all (global.get $cur_out_stream) (local.get $cptr) (local.get $clen)) (then (call $heap_reset (local.get $mark)) (call $drop_input_stream (local.get $is)) (call $drop_incoming_body (local.get $ibody)) (call $drop_incoming_response (local.get $resp)) (call $drop_fir (local.get $fir)) (call $close_output_sinks) (return (i32.const 1)) )) )) )) (call $heap_reset (local.get $mark)) (br $body_loop) ) (else (if (i32.eq (i32.load8_u (i32.const 0x7004)) (i32.const 0)) (then (call $drop_io_error (i32.load (i32.const 0x7008))) (call $heap_reset (local.get $mark)) (call $drop_input_stream (local.get $is)) (call $drop_incoming_body (local.get $ibody)) (call $drop_incoming_response (local.get $resp)) (call $drop_fir (local.get $fir)) (call $close_output_sinks) (if (call $should_show_err) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0F20) (i32.const 51)) (drop (call $stream_flush (global.get $g_stderr))) )) (global.set $stat_elapsed_ns (i64.sub (call $clock_now) (global.get $stat_start_ns))) (call $write_out_format) (return (i32.const 1)) )) (call $heap_reset (local.get $mark)) ) ) ) (call $drop_input_stream (local.get $is)) ) ) (call $drop_incoming_body (local.get $ibody)) )) ) ) (call $drop_incoming_response (local.get $resp)) (call $drop_fir (local.get $fir)) ) ;; end $hop_loop (call $close_output_sinks) (global.set $stat_elapsed_ns (i64.sub (call $clock_now) (global.get $stat_start_ns))) (call $write_out_format) (i32.const 0) ) ;; ========================================================================== ;; 4g. Component Entrypoint ;; ========================================================================== (func (export "run") (result i32) (local $i i32) (local $slot i32) (local $rc i32) (local $mark i32) (global.set $g_stdin (call $get_stdin)) (global.set $g_stdout (call $get_stdout)) (global.set $g_stderr (call $get_stderr)) (if (call $parse_args) (then (drop (call $stream_flush (global.get $g_stderr))) (return (i32.const 1)) )) (if (global.get $opt_help) (then (call $write_out (i32.const 0x0000) (i32.const 1911)) (drop (call $stream_flush (global.get $g_stdout))) (return (i32.const 0)) )) (if (global.get $opt_version) (then (call $write_out (i32.const 0x0900) (i32.const 99)) (drop (call $stream_flush (global.get $g_stdout))) (return (i32.const 0)) )) (if (i32.eq (global.get $cli_url_cnt) (i32.const 0)) (then (call $write_err (i32.const 0x0D00) (i32.const 6)) (call $write_err (i32.const 0x0D08) (i32.const 68)) (drop (call $stream_flush (global.get $g_stderr))) (return (i32.const 1)) )) (call $init_preopens) (local.set $mark (call $heap_mark)) (loop $urls (if (i32.lt_u (local.get $i) (global.get $cli_url_cnt)) (then (local.set $slot (i32.add (i32.const 0x8000) (i32.mul (local.get $i) (i32.const 8)))) (if (call $fetch_url (i32.load (local.get $slot)) (i32.load (i32.add (local.get $slot) (i32.const 4)))) (then (local.set $rc (i32.const 1)) )) (call $heap_reset (local.get $mark)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br $urls) )) ) (drop (call $stream_flush (global.get $g_stdout))) (drop (call $stream_flush (global.get $g_stderr))) (local.get $rc) ) ) ;; ============================================================================ ;; 5. Instantiate Core $curl Module & Export wasi:cli/run@0.2.0 ;; ============================================================================ (core instance $curl_inst (instantiate $curl (with "mem" (instance (export "memory" (memory $mem)) (export "realloc" (func $realloc)) (export "alloc" (func $alloc)) (export "heap_mark" (func $heap_mark)) (export "heap_reset" (func $heap_reset)) )) (with "wasi" (instance (export "err_to_debug_string" (func $err_to_debug_string)) (export "poll_block" (func $poll_block)) (export "in_blocking_read" (func $in_blocking_read)) (export "out_check_write" (func $out_check_write)) (export "out_write" (func $out_write)) (export "out_blocking_flush" (func $out_blocking_flush)) (export "out_subscribe" (func $out_subscribe)) (export "clock_now" (func $clock_now)) (export "get_arguments" (func $get_arguments)) (export "get_stdin" (func $get_stdin)) (export "get_stdout" (func $get_stdout)) (export "get_stderr" (func $get_stderr)) (export "get_directories" (func $get_directories)) (export "desc_open_at" (func $desc_open_at)) (export "desc_read_via_stream" (func $desc_read_via_stream)) (export "desc_write_via_stream" (func $desc_write_via_stream)) (export "new_fields" (func $new_fields)) (export "fields_append" (func $fields_append)) (export "fields_set" (func $fields_set)) (export "fields_delete" (func $fields_delete)) (export "fields_entries" (func $fields_entries)) (export "new_outgoing_request" (func $new_outgoing_request)) (export "outgoing_request_body" (func $outgoing_request_body)) (export "req_set_method" (func $req_set_method)) (export "req_set_path" (func $req_set_path)) (export "req_set_scheme" (func $req_set_scheme)) (export "req_set_authority" (func $req_set_authority)) (export "outgoing_body_write" (func $outgoing_body_write)) (export "outgoing_body_finish" (func $outgoing_body_finish)) (export "new_request_options" (func $new_request_options)) (export "ro_set_connect_timeout" (func $ro_set_connect_timeout)) (export "ro_set_first_byte_timeout" (func $ro_set_first_byte_timeout)) (export "ro_set_between_bytes_timeout" (func $ro_set_between_bytes_timeout)) (export "fir_subscribe" (func $fir_subscribe)) (export "fir_get" (func $fir_get)) (export "resp_status" (func $resp_status)) (export "resp_headers" (func $resp_headers)) (export "resp_consume" (func $resp_consume)) (export "body_stream" (func $body_stream)) (export "http_handle" (func $http_handle)) (export "drop_io_error" (func $drop_io_error)) (export "drop_pollable" (func $drop_pollable)) (export "drop_input_stream" (func $drop_input_stream)) (export "drop_output_stream" (func $drop_output_stream)) (export "drop_descriptor" (func $drop_descriptor)) (export "drop_fields" (func $drop_fields)) (export "drop_outgoing_body" (func $drop_outgoing_body)) (export "drop_outgoing_request" (func $drop_outgoing_request)) (export "drop_request_options" (func $drop_request_options)) (export "drop_incoming_body" (func $drop_incoming_body)) (export "drop_incoming_response" (func $drop_incoming_response)) (export "drop_fir" (func $drop_fir)) )) )) (alias core export $curl_inst "run" (core func $run_core)) (func $run_lifted (result (result)) (canon lift (core func $run_core))) (instance $run_export (export "run" (func $run_lifted))) (export "wasi:cli/run@0.2.0" (instance $run_export)) )