diff --git a/src/mezz/prot-http.r b/src/mezz/prot-http.r index 3b222b0737..35324b7cbd 100644 --- a/src/mezz/prot-http.r +++ b/src/mezz/prot-http.r @@ -31,7 +31,7 @@ REBOL [ ] ] -sync-op: func [port body /local state encoding content-type code-page tmp] [ +sync-op: func [port body /local state encoding content-type code-page tmp][ unless port/state [open port port/state/close?: yes] state: port/state state/awake: :read-sync-awake @@ -41,7 +41,7 @@ sync-op: func [port body /local state encoding content-type code-page tmp] [ ;The timeout should be triggered only when the response from other side exceeds the timeout value. ;--Richard while [not find [ready close] state/state][ - unless port? wait [state/connection port/spec/timeout] [ + unless port? wait [state/connection port/spec/timeout][ state/error: make-http-error "HTTP(s) Timeout" break ] @@ -104,7 +104,7 @@ sync-op: func [port body /local state encoding content-type code-page tmp] [ ] body ] -read-sync-awake: func [event [event!] /local error] [ +read-sync-awake: func [event [event!] /local error][ sys/log/debug 'HTTP ["read-sync-awake:" event/type] switch/default event/type [ connect ready [ @@ -118,7 +118,7 @@ read-sync-awake: func [event [event!] /local error] [ true ] custom [ - if all [event/offset event/offset/x = 300] [ + if all [event/offset event/offset/x = 300][ event/port/state/state: 'redirect return true ] @@ -133,11 +133,11 @@ read-sync-awake: func [event [event!] /local error] [ ] if error? error [do error] ] - ] [ + ][ false ] ] -http-awake: func [event /local port http-port state awake res] [ +http-awake: func [event /local port http-port state awake res][ port: event/port http-port: port/locals state: http-port/state @@ -174,10 +174,10 @@ http-awake: func [event /local port http-port state awake res] [ awake make event! [type: 'error port: http-port] ] reading-data [ - either any [integer? state/info/headers/content-length state/info/headers/transfer-encoding = "chunked"] [ + either any [integer? state/info/headers/content-length state/info/headers/transfer-encoding = "chunked"][ state/error: make-http-error "Server closed connection" awake make event! [type: 'error port: http-port] - ] [ + ][ ;set state to CLOSE so the WAIT loop in 'sync-op can be interrupted --Richard state/state: 'close any [ @@ -192,12 +192,12 @@ http-awake: func [event /local port http-port state awake res] [ if error? state/error [ do state/error ] res ] - ] [true] + ][true] ] make-http-error: func [ "Make an error for the HTTP protocol" message [string! block!] -] [ +][ if block? message [message: ajoin message] make error! [ type: 'Access @@ -213,10 +213,10 @@ make-http-request: func [ headers [block!] "Request headers (set-word! string! pairs)" content [any-string! binary! none!] {Request contents (Content-Length is created automatically). Empty string not exactly like none.} /local result -] [ +][ result: rejoin [ uppercase form method #" " - either file? target [next mold target] [target] + either file? target [next mold target][target] " HTTP/1.1" CRLF ] foreach [word string] headers [ @@ -237,7 +237,7 @@ do-request: func [ "Perform an HTTP request" port [port!] /local spec info -] [ +][ spec: port/spec info: port/state/info spec/headers: body-of make make object! [ @@ -246,7 +246,7 @@ do-request: func [ Accept-Encoding: "gzip,deflate" Host: either not find [80 443] spec/port-id [ rejoin [form spec/host #":" spec/port-id] - ] [ + ][ form spec/host ] User-Agent: any [system/schemes/http/User-Agent "REBOL"] @@ -259,13 +259,13 @@ do-request: func [ write port/state/connection make-http-request spec/method enhex any [spec/path %/] spec/headers spec/content ] -parse-write-dialect: func [port block /local spec] [ +parse-write-dialect: func [port block /local spec][ spec: port/spec parse block [[set block word! (spec/method: block) | (spec/method: 'POST)] - opt [set block [file! | url!] (spec/path: block)] [set block block! (spec/headers: block) | (spec/headers: [])] [set block [any-string! | binary!] (spec/content: block) | (spec/content: none)] + opt [set block [file! | url!] (spec/path: block)][set block block! (spec/headers: block) | (spec/headers: [])][set block [any-string! | binary!] (spec/content: block) | (spec/content: none)] ] ] -check-response: func [port /local conn res headers d1 d2 line info state awake spec] [ +check-response: func [port /local conn res headers d1 d2 line info state awake spec][ state: port/state conn: state/connection info: state/info @@ -288,7 +288,7 @@ check-response: func [port /local conn res headers d1 d2 line info state awake s sys/log/debug 'HTML "server using malformed line separator of #{0A0A}" ] ] - ] [ + ][ info/response-line: line: to string! copy/part conn/data d1 sys/log/more 'HTTP line ;probe to-string copy/part d1 d2 @@ -356,11 +356,11 @@ check-response: func [port /local conn res headers d1 d2 line info state awake s state/state: 'ready res: awake make event! [type: 'done port: port] unless res [res: awake make event! [type: 'ready port: port]] - ] [ + ][ res: check-data port ;?? res ;?? state/state - if all [not res state/state = 'ready] [ + if all [not res state/state = 'ready][ res: awake make event! [type: 'done port: port] unless res [res: awake make event! [type: 'ready port: port]] ] @@ -370,7 +370,7 @@ check-response: func [port /local conn res headers d1 d2 line info state awake s either spec/method = 'HEAD [ state/state: 'ready res: awake make event! [type: 'custom port: port code: 0] - ] [ + ][ res: check-data port unless open? port [ ;NOTE some servers(e.g. yahoo.com) don't supply content-data in the redirect header so the state/state can be left in 'reading-data after check-data call @@ -382,7 +382,7 @@ check-response: func [port /local conn res headers d1 d2 line info state awake s ;?? headers ;?? state/state - if all [not res state/state = 'ready] [ + if all [not res state/state = 'ready][ either all [ any [ find [get head] spec/method @@ -392,9 +392,9 @@ check-response: func [port /local conn res headers d1 d2 line info state awake s ] ] in headers 'Location - ] [ + ][ return awake make event! [type: 'custom port: port code: 300] - ] [ + ][ state/error: make-http-error "Redirect requires manual intervention" res: awake make event! [type: 'error port: port] ] @@ -403,7 +403,7 @@ check-response: func [port /local conn res headers d1 d2 line info state awake s unauthorized client-error server-error proxy-auth [ either spec/method = 'HEAD [ state/state: 'ready - ] [ + ][ check-data port ] ] @@ -456,7 +456,7 @@ http-response-headers: context [ Last-Modified: none ] -do-redirect: func [port [port!] new-uri [url! string! file!] /local spec state] [ +do-redirect: func [port [port!] new-uri [url! string! file!] /local spec state][ spec: port/spec state: port/state @@ -506,7 +506,7 @@ do-redirect: func [port [port!] new-uri [url! string! file!] /local spec state] open port ] -check-data: func [port /local headers res data out chunk-size mk1 mk2 trailer state conn] [ +check-data: func [port /local headers res data out chunk-size mk1 mk2 trailer state conn][ state: port/state headers: state/info/headers conn: state/connection @@ -525,13 +525,13 @@ check-data: func [port /local headers res data out chunk-size mk1 mk2 trailer st either parse/all data [ copy chunk-size some hex-digits crlfbin mk1: to end - ] [ + ][ chunk-size: to integer! to issue! to string! chunk-size sys/log/more 'HTTP ["chunk-size:^[[m" chunk-size] either chunk-size = 0 [ if parse/all mk1 [ crlfbin (trailer: "") to end | copy trailer to crlf2bin to end - ] [ + ][ trailer: construct trailer append headers body-of trailer state/state: 'ready @@ -539,18 +539,18 @@ check-data: func [port /local headers res data out chunk-size mk1 mk2 trailer st clear data ] true - ] [ + ][ either parse/all mk1 [ chunk-size skip mk2: crlfbin to end - ] [ + ][ insert/part tail out mk1 mk2 remove/part data skip mk2 2 empty? data - ] [ + ][ true ] ] - ] [ + ][ true ] ] @@ -565,7 +565,7 @@ check-data: func [port /local headers res data out chunk-size mk1 mk2 trailer st state/state: 'ready conn/data: make binary! 32000 ;@@ Oldes: why not just none? res: state/awake make event! [type: 'custom port: port code: 0] - ] [ + ][ ;Awake from the WAIT loop to prevent timeout when reading big data. --Richard res: true ] @@ -602,24 +602,24 @@ sys/make-scheme [ actor: [ read: func [ port [port!] - ] [ + ][ sys/log/debug 'HTTP "read" either any-function? :port/awake [ unless open? port [cause-error 'Access 'not-open port/spec/ref] if port/state/state <> 'ready [http-error "Port not ready"] port/state/awake: :port/awake do-request port - ] [ + ][ sync-op port [] ] ] write: func [ port [port!] value - ] [ + ][ sys/log/debug 'HTTP "write" ;?? port - unless any [block? :value binary? :value any-string? :value] [value: form :value] + unless any [block? :value binary? :value any-string? :value][value: form :value] unless block? value [value: reduce [[Content-Type: "application/x-www-form-urlencoded; charset=utf-8"] value]] either any-function? :port/awake [ @@ -628,14 +628,14 @@ sys/make-scheme [ port/state/awake: :port/awake parse-write-dialect port value do-request port - ] [ + ][ sync-op port [parse-write-dialect port value] ] ] open: func [ port [port!] /local conn - ] [ + ][ sys/log/debug 'HTTP ["open, state:" port/state] if port/state [return port] if none? port/spec/host [http-error "Missing host address"] @@ -666,12 +666,12 @@ sys/make-scheme [ ] open?: func [ port [port!] - ] [ + ][ found? all [port/state open? port/state/connection] ] close: func [ port [port!] - ] [ + ][ sys/log/debug 'HTTP "close" if port/state [ close port/state/connection @@ -682,10 +682,10 @@ sys/make-scheme [ ] copy: func [ port [port!] - ] [ - either all [port/spec/method = 'HEAD port/state] [ + ][ + either all [port/spec/method = 'HEAD port/state][ reduce bind [name size date] port/state/info - ] [ + ][ if port/data [copy port/data] ] ] @@ -694,8 +694,8 @@ sys/make-scheme [ /mode field [word! block! none!] /local error state result - ] [ - if all [mode none? field] [ return words-of system/schemes/http/info] + ][ + if all [mode none? field][ return words-of system/schemes/http/info] if none? state: port/state [ open port ;there is port opening in sync-op, but it would also close the port later and so clear the state attempt [sync-op port [parse-write-dialect port [HEAD]]] @@ -725,8 +725,8 @@ sys/make-scheme [ ] length?: func [ port [port!] - ] [ - either port/data [length? port/data] [0] + ][ + either port/data [length? port/data][0] ] ] User-Agent: none diff --git a/src/mezz/prot-tls.r b/src/mezz/prot-tls.r index 8cceecd9be..9d5bbb6785 100644 --- a/src/mezz/prot-tls.r +++ b/src/mezz/prot-tls.r @@ -340,7 +340,7 @@ TLS-init-cipher-suite: func [ make-TLS-error: func [ "Make an error for the TLS protocol" message [string! block!] -] [ +][ if block? message [message: ajoin message] make error! [ type: 'Access @@ -409,7 +409,7 @@ TLS-update-messages-hash: function [ client-hello: function [ ctx [object!] -] [ +][ change-state ctx 'CLIENT_HELLO with ctx [ @@ -478,7 +478,7 @@ client-hello: function [ client-key-exchange: function [ ctx [object!] -] [ +][ ;log-debug ["client-key-exchange -> method:" ctx/key-method] change-state ctx 'CLIENT_KEY_EXCHANGE @@ -614,7 +614,7 @@ client-key-exchange: function [ change-cipher-spec: function [ ctx [object!] -] [ +][ ;@@ actually this is not just state, but its own "protocol" ;@@ https://tools.ietf.org/html/rfc5246#section-7.1 @@ -635,7 +635,7 @@ change-cipher-spec: function [ application-data: func [ ctx [object!] message [binary! string!] -] [ +][ log-more "application-data" ;prin "unencrypted: " ?? message message: encrypt-data ctx to binary! message @@ -651,7 +651,7 @@ application-data: func [ alert-close-notify: func [ ctx [object!] -] [ +][ ;@@ Not used/tested yet! It should be replaced with ALERT-notify with CLOSE as possible type log-more "alert-close-notify" message: encrypt-data ctx #{0100} ; close notify @@ -666,7 +666,7 @@ alert-close-notify: func [ finished: function [ ctx [object!] -] [ +][ log-info ["FINISHED^[[22m write sequence:" ctx/seq-write] ctx/seq-write: 0 @@ -694,7 +694,7 @@ encrypt-handshake-msg: function [ unencrypted [binary!] /local plain-msg -] [ +][ log-more ["W[" ctx/seq-write "] encrypting-handshake-msg"] encrypted: encrypt-data/type ctx unencrypted 22 @@ -760,7 +760,7 @@ encrypt-data: function [ content [binary!] /type msg-type [integer!] "application data is default" -] [ +][ ;log-debug "--encrypt-data--" ;? ctx msg-type: any [msg-type 23] ;-- default application @@ -854,7 +854,7 @@ decrypt-data: func [ data [binary!] /local crypt-data -] [ +][ switch ctx/crypt-method [ RC4_128 [ unless ctx/decrypt-stream [ @@ -911,14 +911,14 @@ prf: function [ p-md5: copy #{} a: seed ; A(0) - while [output-length > length? p-md5] [ + while [output-length > length? p-md5][ a: checksum/method/key a 'md5 s-1 ; A(n) append p-md5 checksum/method/key rejoin [a seed] 'md5 s-1 ] p-sha1: copy #{} a: seed ; A(0) - while [output-length > length? p-sha1] [ + while [output-length > length? p-sha1][ a: checksum/method/key a 'sha1 s-2 ; A(n) append p-sha1 checksum/method/key rejoin [a seed] 'sha1 s-2 ] @@ -936,7 +936,7 @@ prf: function [ p-sha256: make binary! output-length a: seed ; A(0) - while [output-length >= length? p-sha256] [ + while [output-length >= length? p-sha256][ a: checksum/method/key a 'sha256 secret append p-sha256 checksum/method/key rejoin [a seed] 'sha256 secret ;?? p-sha256 @@ -952,7 +952,7 @@ do-commands: func [ commands [block!] /no-wait /local arg cmd -] [ +][ binary/init ctx/out none ;reset output buffer parse commands [ @@ -980,7 +980,7 @@ do-commands: func [ unless no-wait [ log-more "Waiting for responses" - unless port? wait [ctx/connection 30] [ + unless port? wait [ctx/connection 30][ log-error "Timeout" ;? ctx ? ctx/connection @@ -1007,7 +1007,7 @@ make-TLS-ctx: does [ context [ bin: binary 64 ;temporary binary port-data: make binary! 32000 ;this holds received decrypted application data - rest: make binary! 8 ;packet may not e fully processed, this value is used to keep temporary leftover + rest: make binary! 8 ;packet may not be fully processed, this value is used to keep temporary leftover reading?: false ;if client is reading or writing data ;server?: false ;always FALSE now as we have just a client protocol: none ;current protocol state. One of: [HANDSHAKE APPLICATION ALERT] @@ -1066,7 +1066,7 @@ make-TLS-ctx: does [ context [ TLS-init: func [ "Resets existing TLS context" ctx [object!] -] [ +][ ctx/seq-read: ctx/seq-write: 0 ctx/protocol: ctx/state: ctx/state-prev: none ctx/cipher-spec-set: 0 ;no encryption yet @@ -1088,7 +1088,7 @@ TLS-init: func [ TLS-read-data: function [ ctx [object!] port-data [binary!] -] [ +][ ;log-more ["read-data:^[[1m" length? port-data "^[[22mbytes"] inp: ctx/in @@ -1277,7 +1277,7 @@ TLS-read-handshake-message: function [ if any [ none? tmp server-version > version - ] [ + ][ return *Alert/Protocol_version ] @@ -1319,7 +1319,7 @@ TLS-read-handshake-message: function [ ] ] false ;= no error - ] [; ctx + ][; ctx ; WITH block catches RETURNs so just throw it again return ctx/critical-error ] @@ -1487,7 +1487,7 @@ send-event: function[ ] ] -TLS-awake: function [event [event!]] [ +TLS-awake: function [event [event!]][ log-more ["AWAKE:^[[1m" event/type] port: event/port TLS-port: port/locals @@ -1496,7 +1496,7 @@ TLS-awake: function [event [event!]] [ if all [ TLS-port/state/protocol = 'APPLICATION not port/data - ] [ + ][ ; reset the data field when interleaving port r/w states ;@@ TODO: review this part log-debug ["reseting data -> " mold TLS-port/data] @@ -1586,14 +1586,14 @@ sys/make-scheme [ port [port!] /local resp data msg - ] [ + ][ log-more "READ" ;? port read port/state/connection return port ] - write: func [port [port!] value [any-type!]] [ + write: func [port [port!] value [any-type!]][ log-more "WRITE" ;?? port/state/protocol if port/state/protocol = 'APPLICATION [ ;encrypted-handshake? @@ -1604,7 +1604,7 @@ sys/make-scheme [ ] ] - open: func [port [port!] /local conn] [ + open: func [port [port!] /local conn][ log-more "OPEN" if port/state [return port] @@ -1626,10 +1626,10 @@ sys/make-scheme [ open conn port ] - open?: func [port [port!]] [ + open?: func [port [port!]][ found? all [port/state open? port/state/connection] ] - close: func [port [port!] /local ctx] [ + close: func [port [port!] /local ctx][ log-more "CLOSE" ;? port ? port/scheme unless port/state [return port] @@ -1676,14 +1676,14 @@ sys/make-scheme [ port/state: none port ] - copy: func [port [port!]] [ + copy: func [port [port!]][ if port/data [copy port/data] ] - query: func [port [port!]] [ + query: func [port [port!]][ all [port/state query port/state/connection] ] - length?: func [port [port!]] [ - either port/data [length? port/data] [0] + length?: func [port [port!]][ + either port/data [length? port/data][0] ] ] set-verbose: :tls-verbosity