diff options
| author | bptato <nincsnevem662@gmail.com> | 2025-04-14 19:34:39 +0200 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2025-04-14 19:34:39 +0200 |
| commit | fa4d825bb6c678653ddb675bd511d53a52df934f (patch) | |
| tree | a66aa597b81ea463b349ea28f1a1b497e2914509 | |
| parent | fb20b9d9dfd09590a06b7d6b0c11abd1a30e854f (diff) | |
| download | chawan-fa4d825bb6c678653ddb675bd511d53a52df934f.tar.gz | |
http: replace with newhttp
I think it works well enough at this point.
If it doesn't, somebody will complain :)
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | adapter/protocol/http.nim | 995 | ||||
| -rw-r--r-- | adapter/protocol/tinfl.h (renamed from bonus/newhttp/tinfl.h) | 0 | ||||
| -rw-r--r-- | bonus/Makefile | 11 | ||||
| -rw-r--r-- | bonus/README.md | 18 | ||||
| -rw-r--r-- | bonus/curlhttp.nim | 655 | ||||
| -rw-r--r-- | bonus/newhttp/http.nim | 377 | ||||
| -rw-r--r-- | doc/protocols.md | 37 | ||||
| -rw-r--r-- | res/license.md | 19 |
10 files changed, 1059 insertions, 1056 deletions
@@ -118,7 +118,7 @@ lcgi_ssl = $(lcgi) adapter/protocol/lcgi_ssl.nim sandbox = src/utils/sandbox.nim $(chaseccomp) $(OUTDIR_CGI_BIN)/man: $(twtstr) -$(OUTDIR_CGI_BIN)/http: $(sandbox) +$(OUTDIR_CGI_BIN)/http: $(sandbox) $(lcgi_ssl) $(OUTDIR_CGI_BIN)/file: $(twtstr) $(OUTDIR_CGI_BIN)/ftp: $(lcgi) $(OUTDIR_CGI_BIN)/sftp: $(lcgi) $(twtstr) @@ -24,7 +24,6 @@ Note: a POSIX-compliant operating system is required. before compiling, or (preferably) use a nightly build of the Nim compiler. 3. Install the following dependencies: - * [libcurl](https://curl.se/libcurl/) * [OpenSSL](https://www.openssl.org/) (or [LibreSSL](https://www.libressl.org/)) * [libssh2](https://libssh2.org/) diff --git a/adapter/protocol/http.nim b/adapter/protocol/http.nim index 7a612e4a..e10c061b 100644 --- a/adapter/protocol/http.nim +++ b/adapter/protocol/http.nim @@ -1,656 +1,377 @@ -when NimMajor >= 2: - import std/envvars -else: - import std/os +import std/envvars +import std/options import std/posix import std/strutils -import utils/sandbox - -const curllib = (func(): string = - const curlLibName {.strdefine.} = "" - when curlLibName != "": - return curlLibName - elif defined(macosx): - return "libcurl(|.4|.4.8.0).dylib" - else: # assume elf - return "libcurl.so(|.4|.4.8.0)" -)() - -const - CURL_GLOBAL_SSL* = 1 shl 0 # no purpose since 7.57.0 - CURL_GLOBAL_WIN32* = 1 shl 1 - CURL_GLOBAL_ALL* = CURL_GLOBAL_SSL or CURL_GLOBAL_WIN32 - CURL_GLOBAL_NOTHING* = 0 - CURL_GLOBAL_DEFAULT* = CURL_GLOBAL_ALL - CURL_GLOBAL_ACK_EINTR* = 1 shl 2 -const - CURLOPTTYPE_LONG = 0 - CURLOPTTYPE_OBJECTPOINT = 10000 - CURLOPTTYPE_FUNCTIONPOINT = 20000 - CURLOPTTYPE_OFF_T = 30000 - CURLOPTTYPE_BLOB = 40000 - -const - CURLOPTTYPE_STRINGPOINT = CURLOPTTYPE_OBJECTPOINT - CURLOPTTYPE_SLISTPOINT = CURLOPTTYPE_OBJECTPOINT - CURLOPTTYPE_CBPOINT = CURLOPTTYPE_OBJECTPOINT - CURLOPTTYPE_VALUES = CURLOPTTYPE_LONG +import io/dynstream +import types/opt +import utils/sandbox -const - CURLINFO_STRING = 0x100000 - CURLINFO_LONG = 0x200000 - CURLINFO_DOUBLE = 0x300000 - CURLINFO_SLIST = 0x400000 - CURLINFO_PTR = 0x400000 # same as SLIST - CURLINFO_SOCKET = 0x500000 - CURLINFO_OFF_T = 0x600000 - CURLINFO_MASK {.used.} = 0x0fffff - CURLINFO_TYPEMASK {.used.} = 0xf00000 +import adapter/protocol/lcgi_ssl +# tinfl bindings, see tinfl.h for details const - CURL_WAIT_POLLIN* = 0x0001 - CURL_WAIT_POLLPRI* = 0x0002 - CURL_WAIT_POLLOUT* = 0x0004 + TINFL_MAX_HUFF_TABLES = 3 + TINFL_MAX_HUFF_SYMBOLS_0 = 288 + TINFL_MAX_HUFF_SYMBOLS_1 = 32 + TINFL_FAST_LOOKUP_BITS = 10 + TINFL_FAST_LOOKUP_SIZE = 1 shl TINFL_FAST_LOOKUP_BITS -# CURLU -const - CURLU_DEFAULT_PORT* = (1 shl 0) # return default port number - CURLU_NO_DEFAULT_PORT* = (1 shl 1) # act as if no port number was set, - # if the port number matches the - # default for the scheme - CURLU_DEFAULT_SCHEME* = (1 shl 2) # return default scheme if missing - CURLU_NON_SUPPORT_SCHEME* = (1 shl 3) # allow non-supported scheme - CURLU_PATH_AS_IS* = (1 shl 4) # leave dot sequences - CURLU_DISALLOW_USER* = (1 shl 5) # no user+password allowed - CURLU_URLDECODE* = (1 shl 6) # URL decode on get - CURLU_URLENCODE* = (1 shl 7) # URL encode on set - CURLU_APPENDQUERY* = (1 shl 8) # append a form style part - CURLU_GUESS_SCHEME* = (1 shl 9) # legacy curl-style guessing - CURLU_NO_AUTHORITY* = (1 shl 10) # Allow empty authority when the scheme - # is unknown. - CURLU_ALLOW_SPACE* = (1 shl 11) # Allow spaces in the URL - CURLU_PUNYCODE* = (1 shl 12) # get the host name in punycode - CURLU_PUNY2IDN* = (1 shl 13) # punycode => IDN conversion +const TINFL_LZ_DICT_SIZE = 32768 const - CURLH_HEADER* = 1 shl 0 - CURLH_TRAILER* = 1 shl 1 - CURLH_CONNECT* = 1 shl 2 - CURLH_1XX* = 1 shl 3 - CURLH_PSEUDO* = 1 shl 4 - -{.push cdecl, dynlib: curllib.} - -type - CURL* = distinct pointer - CURLM* = distinct pointer - CURLU* = distinct pointer - - curl_mime_struct = object - curl_mime* = ptr curl_mime_struct - curl_mimepart_struct = object - curl_mimepart* = ptr curl_mimepart_struct - curl_slist_struct = object - curl_slist* = ptr curl_slist_struct - curl_socket_t = cint - curl_waitfd* = object - fd*: curl_socket_t - events*: cshort - revents*: cshort # this is, in fact, supported. - CURLMsg_data {.union.} = object - whatever: pointer - result*: CURLcode - CURLMsg_struct = object - msg*: CURLMSG_E - easy_handle*: CURL - data*: CURLMsg_data - CURLMsg* = ptr CURLMsg_struct - - CURLoption* {.size: sizeof(cint).} = enum - # Long - CURLOPT_PORT = CURLOPTTYPE_LONG + 3 - CURLOPT_SSLVERSION = CURLOPTTYPE_VALUES + 32 - CURLOPT_TIMECONDITION = CURLOPTTYPE_VALUES + 33 - CURLOPT_POST = CURLOPTTYPE_LONG + 47 - CURLOPT_DIRLISTONLY = CURLOPTTYPE_LONG + 48 - CURLOPT_FOLLOWLOCATION = CURLOPTTYPE_LONG + 52 - CURLOPT_POSTFIELDSIZE = CURLOPTTYPE_LONG + 60 - CURLOPT_SSL_VERIFYPEER = CURLOPTTYPE_LONG + 64 - CURLOPT_HTTPGET = CURLOPTTYPE_LONG + 80 - CURLOPT_SSL_VERIFYHOST = CURLOPTTYPE_LONG + 81 - CURLOPT_NOSIGNAL = CURLOPTTYPE_LONG + 99 - CURLOPT_FTP_FILEMETHOD = CURLOPTTYPE_VALUES + 138 - CURLOPT_CONNECT_ONLY = CURLOPTTYPE_LONG + 141 - CURLOPT_SUPPRESS_CONNECT_HEADERS = CURLOPTTYPE_LONG + 265 - - # Objectpoint - CURLOPT_WRITEDATA = CURLOPTTYPE_CBPOINT + 1 - CURLOPT_URL = CURLOPTTYPE_STRINGPOINT + 2 - CURLOPT_PROXY = CURLOPTTYPE_STRINGPOINT + 4 - CURLOPT_ERRORBUFFER = CURLOPTTYPE_OBJECTPOINT + 10 - CURLOPT_POSTFIELDS = CURLOPTTYPE_OBJECTPOINT + 15 - CURLOPT_HTTPHEADER = CURLOPTTYPE_SLISTPOINT + 23 - CURLOPT_KEYPASSWD = CURLOPTTYPE_STRINGPOINT + 26 - CURLOPT_HEADERDATA = CURLOPTTYPE_CBPOINT + 29 - CURLOPT_ACCEPT_ENCODING = CURLOPTTYPE_STRINGPOINT + 102 - CURLOPT_SSH_PUBLIC_KEYFILE = CURLOPTTYPE_STRINGPOINT + 152 - CURLOPT_SSH_PRIVATE_KEYFILE = CURLOPTTYPE_STRINGPOINT + 153 - CURLOPT_MIMEPOST = CURLOPTTYPE_OBJECTPOINT + 269 - CURLOPT_CURLU = CURLOPTTYPE_OBJECTPOINT + 282 - CURLOPT_PREREQDATA = CURLOPTTYPE_CBPOINT + 313 - - # Functionpoint - CURLOPT_WRITEFUNCTION = CURLOPTTYPE_FUNCTIONPOINT + 11 - CURLOPT_READFUNCTION = CURLOPTTYPE_FUNCTIONPOINT + 12 - CURLOPT_HEADERFUNCTION = CURLOPTTYPE_FUNCTIONPOINT + 79 - CURLOPT_PREREQFUNCTION = CURLOPTTYPE_FUNCTIONPOINT + 312 - - # Off-t - CURLOPT_INFILESIZE_LARGE = CURLOPTTYPE_OFF_T + 115 - CURLOPT_RESUME_FROM_LARGE = CURLOPTTYPE_OFF_T + 116 - CURLOPT_POSTFIELDSIZE_LARGE = CURLOPTTYPE_OFF_T + 120 - - # Blob - CURLOPT_SSLCERT_BLOB = CURLOPTTYPE_BLOB + 291 - CURLOPT_SSLKEY_BLOB = CURLOPTTYPE_BLOB + 292 - CURLOPT_PROXY_SSLCERT_BLOB = CURLOPTTYPE_BLOB + 293 - CURLOPT_PROXY_SSLKEY_BLOB = CURLOPTTYPE_BLOB + 294 - CURLOPT_ISSUECERT_BLOB = CURLOPTTYPE_BLOB + 295 - - CURLINFO* {.size: sizeof(cint).} = enum - CURLINFO_NONE # first, never use this - - # String - CURLINFO_REDIRECT_URL = CURLINFO_STRING + 31 - - # Long - CURLINFO_RESPONSE_CODE = CURLINFO_LONG + 2 - - # Double - CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE + 3 - - # S-list - CURLINFO_SSL_ENGINES = CURLINFO_SLIST + 27 - CURLINFO_COOKIELIST = CURLINFO_SLIST + 28 - - # Pointer - CURLINFO_CERTINFO = CURLINFO_PTR + 34 - CURLINFO_TLS_SESSION = CURLINFO_PTR + 43 - CURLINFO_TLS_SSL_PTR = CURLINFO_PTR + 45 - - # Socket - CURLINFO_ACTIVESOCKET = CURLINFO_SOCKET + 44 - - # Off_t - CURLINFO_SIZE_UPLOAD_T = CURLINFO_OFF_T + 7 - CURLINFO_SIZE_DOWNLOAD_T = CURLINFO_OFF_T + 9 - - CURLcode* {.size: sizeof(cint).} = enum - CURLE_OK = 0, - CURLE_UNSUPPORTED_PROTOCOL, # 1 - CURLE_FAILED_INIT, # 2 - CURLE_URL_MALFORMAT, # 3 - CURLE_NOT_BUILT_IN, # 4 - [was obsoleted in August 2007 for - # 7.17.0, reused in April 2011 for 7.21.5] - CURLE_COULDNT_RESOLVE_PROXY, # 5 - CURLE_COULDNT_RESOLVE_HOST, # 6 - CURLE_COULDNT_CONNECT, # 7 - CURLE_WEIRD_SERVER_REPLY, # 8 - CURLE_REMOTE_ACCESS_DENIED, # 9 a service was denied by the server - # due to lack of access - when login fails - # this is not returned. - CURLE_FTP_ACCEPT_FAILED, # 10 - [was obsoleted in April 2006 for - # 7.15.4, reused in Dec 2011 for 7.24.0] - CURLE_FTP_WEIRD_PASS_REPLY, # 11 - CURLE_FTP_ACCEPT_TIMEOUT, # 12 - timeout occurred accepting server - # [was obsoleted in August 2007 for 7.17.0, - # reused in Dec 2011 for 7.24.0] - CURLE_FTP_WEIRD_PASV_REPLY, # 13 - CURLE_FTP_WEIRD_227_FORMAT, # 14 - CURLE_FTP_CANT_GET_HOST, # 15 - CURLE_HTTP2, # 16 - A problem in the http2 framing layer. - # [was obsoleted in August 2007 for 7.17.0, - # reused in July 2014 for 7.38.0] - CURLE_FTP_COULDNT_SET_TYPE, # 17 - CURLE_PARTIAL_FILE, # 18 - CURLE_FTP_COULDNT_RETR_FILE, # 19 - CURLE_OBSOLETE20, # 20 - NOT USED - CURLE_QUOTE_ERROR, # 21 - quote command failure - CURLE_HTTP_RETURNED_ERROR, # 22 - CURLE_WRITE_ERROR, # 23 - CURLE_OBSOLETE24, # 24 - NOT USED - CURLE_UPLOAD_FAILED, # 25 - failed upload "command" - CURLE_READ_ERROR, # 26 - couldn't open/read from file - CURLE_OUT_OF_MEMORY, # 27 - CURLE_OPERATION_TIMEDOUT, # 28 - the timeout time was reached - CURLE_OBSOLETE29, # 29 - NOT USED - CURLE_FTP_PORT_FAILED, # 30 - FTP PORT operation failed - CURLE_FTP_COULDNT_USE_REST, # 31 - the REST command failed - CURLE_OBSOLETE32, # 32 - NOT USED - CURLE_RANGE_ERROR, # 33 - RANGE "command" didn't work - CURLE_HTTP_POST_ERROR, # 34 - CURLE_SSL_CONNECT_ERROR, # 35 - wrong when connecting with SSL - CURLE_BAD_DOWNLOAD_RESUME, # 36 - couldn't resume download - CURLE_FILE_COULDNT_READ_FILE, # 37 - CURLE_LDAP_CANNOT_BIND, # 38 - CURLE_LDAP_SEARCH_FAILED, # 39 - CURLE_OBSOLETE40, # 40 - NOT USED - CURLE_FUNCTION_NOT_FOUND, # 41 - NOT USED starting with 7.53.0 - CURLE_ABORTED_BY_CALLBACK, # 42 - CURLE_BAD_FUNCTION_ARGUMENT, # 43 - CURLE_OBSOLETE44, # 44 - NOT USED - CURLE_INTERFACE_FAILED, # 45 - CURLOPT_INTERFACE failed - CURLE_OBSOLETE46, # 46 - NOT USED - CURLE_TOO_MANY_REDIRECTS, # 47 - catch endless re-direct loops - CURLE_UNKNOWN_OPTION, # 48 - User specified an unknown option - CURLE_SETOPT_OPTION_SYNTAX, # 49 - Malformed setopt option - CURLE_OBSOLETE50, # 50 - NOT USED - CURLE_OBSOLETE51, # 51 - NOT USED - CURLE_GOT_NOTHING, # 52 - when this is a specific error - CURLE_SSL_ENGINE_NOTFOUND, # 53 - SSL crypto engine not found - CURLE_SSL_ENGINE_SETFAILED, # 54 - can not set SSL crypto engine as - # default - CURLE_SEND_ERROR, # 55 - failed sending network data - CURLE_RECV_ERROR, # 56 - failure in receiving network data - CURLE_OBSOLETE57, # 57 - NOT IN USE - CURLE_SSL_CERTPROBLEM, # 58 - problem with the local certificate - CURLE_SSL_CIPHER, # 59 - couldn't use specified cipher - CURLE_PEER_FAILED_VERIFICATION, # 60 - peer's certificate or fingerprint - # wasn't verified fine - CURLE_BAD_CONTENT_ENCODING, # 61 - Unrecognized/bad encoding - CURLE_OBSOLETE62, # 62 - NOT IN USE since 7.82.0 - CURLE_FILESIZE_EXCEEDED, # 63 - Maximum file size exceeded - CURLE_USE_SSL_FAILED, # 64 - Requested FTP SSL level failed - CURLE_SEND_FAIL_REWIND, # 65 - Sending the data requires a rewind - # that failed - CURLE_SSL_ENGINE_INITFAILED, # 66 - failed to initialise ENGINE - CURLE_LOGIN_DENIED, # 67 - user, password or similar was not - # accepted and we failed to login - CURLE_TFTP_NOTFOUND, # 68 - file not found on server - CURLE_TFTP_PERM, # 69 - permission problem on server - CURLE_REMOTE_DISK_FULL, # 70 - out of disk space on server - CURLE_TFTP_ILLEGAL, # 71 - Illegal TFTP operation - CURLE_TFTP_UNKNOWNID, # 72 - Unknown transfer ID - CURLE_REMOTE_FILE_EXISTS, # 73 - File already exists - CURLE_TFTP_NOSUCHUSER, # 74 - No such user - CURLE_CONV_FAILED, # 75 - conversion failed - CURLE_OBSOLETE76, # 76 - NOT IN USE since 7.82.0 - CURLE_SSL_CACERT_BADFILE, # 77 - could not load CACERT file, missing - # or wrong format - CURLE_REMOTE_FILE_NOT_FOUND, # 78 - remote file not found - CURLE_SSH, # 79 - error from the SSH layer, somewhat - # generic so the error message will be of - # interest when this has happened - - CURLE_SSL_SHUTDOWN_FAILED, # 80 - Failed to shut down the SSL - # connection - CURLE_AGAIN, # 81 - socket is not ready for send/recv, - # wait till it's ready and try again (Added - # in 7.18.2) - CURLE_SSL_CRL_BADFILE, # 82 - could not load CRL file, missing or - # wrong format (Added in 7.19.0) - CURLE_SSL_ISSUER_ERROR, # 83 - Issuer check failed. (Added in - # 7.19.0) - CURLE_FTP_PRET_FAILED, # 84 - a PRET command failed - CURLE_RTSP_CSEQ_ERROR, # 85 - mismatch of RTSP CSeq numbers - CURLE_RTSP_SESSION_ERROR, # 86 - mismatch of RTSP Session Ids - CURLE_FTP_BAD_FILE_LIST, # 87 - unable to parse FTP file list - CURLE_CHUNK_FAILED, # 88 - chunk callback reported error - CURLE_NO_CONNECTION_AVAILABLE, # 89 - No connection available, the - # session will be queued - CURLE_SSL_PINNEDPUBKEYNOTMATCH, # 90 - specified pinned public key did not - # match - CURLE_SSL_INVALIDCERTSTATUS, # 91 - invalid certificate status - CURLE_HTTP2_STREAM, # 92 - stream error in HTTP/2 framing layer - CURLE_RECURSIVE_API_CALL, # 93 - an api function was called from - # inside a callback - CURLE_AUTH_ERROR, # 94 - an authentication function returned an - # error - CURLE_HTTP3, # 95 - An HTTP/3 layer problem - CURLE_QUIC_CONNECT_ERROR, # 96 - QUIC connection error - CURLE_PROXY, # 97 - proxy handshake error - CURLE_SSL_CLIENTCERT, # 98 - client-side certificate required - CURLE_UNRECOVERABLE_POLL, # 99 - poll/select returned fatal error - CURL_LAST # never use! - - curl_ftpmethod* {.size: sizeof(clong).} = enum - CURLFTPMETHOD_DEFAULT, # let libcurl pick - CURLFTPMETHOD_MULTICWD, # single CWD operation for each path part - CURLFTPMETHOD_NOCWD, # no CWD at all - CURLFTPMETHOD_SINGLECWD, # one CWD to full dir, then work on file - - CURLMcode* {.size: sizeof(cint).} = enum - CURLM_CALL_MULTI_PERFORM = -1, # please call curl_multi_perform() or - # curl_multi_socket*() soon - CURLM_OK, - CURLM_BAD_HANDLE, # the passed-in handle is not a valid CURLM handle - CURLM_BAD_EASY_HANDLE, # an easy handle was not good/valid - CURLM_OUT_OF_MEMORY, # if you ever get this, you're in deep sh*t - CURLM_INTERNAL_ERROR, # this is a libcurl bug - CURLM_BAD_SOCKET, # the passed in socket argument did not match - CURLM_UNKNOWN_OPTION, # curl_multi_setopt() with unsupported option - CURLM_ADDED_ALREADY, # an easy handle already added to a multi handle was - # attempted to get added - again - CURLM_RECURSIVE_API_CALL, # an api function was called from inside a - # callback - CURLM_WAKEUP_FAILURE, # wakeup is unavailable or failed - CURLM_BAD_FUNCTION_ARGUMENT, # function called with a bad parameter - CURLM_ABORTED_BY_CALLBACK, - CURLM_UNRECOVERABLE_POLL, - CURLM_LAST - - CURLMSG_E* {.size: sizeof(cint).} = enum - CURLMSG_NONE # first, not used - CURLMSG_DONE # This easy handle has completed. 'result' contains - # the CURLcode of the transfer - CURLMSG_LAST # last, not used - - CURLUcode* {.size: sizeof(cint).} = enum - CURLUE_OK, - CURLUE_BAD_HANDLE # 1 - CURLUE_BAD_PARTPOINTER # 2 - CURLUE_MALFORMED_INPUT # 3 - CURLUE_BAD_PORT_NUMBER # 4 - CURLUE_UNSUPPORTED_SCHEME # 5 - CURLUE_URLDECODE # 6 - CURLUE_OUT_OF_MEMORY # 7 - CURLUE_USER_NOT_ALLOWED # 8 - CURLUE_UNKNOWN_PART # 9 - CURLUE_NO_SCHEME # 10 - CURLUE_NO_USER # 11 - CURLUE_NO_PASSWORD # 12 - CURLUE_NO_OPTIONS # 13 - CURLUE_NO_HOST # 14 - CURLUE_NO_PORT # 15 - CURLUE_NO_QUERY # 16 - CURLUE_NO_FRAGMENT # 17 - CURLUE_NO_ZONEID # 18 - CURLUE_BAD_FILE_URL # 19 - CURLUE_BAD_FRAGMENT # 20 - CURLUE_BAD_HOSTNAME # 21 - CURLUE_BAD_IPV6 # 22 - CURLUE_BAD_LOGIN # 23 - CURLUE_BAD_PASSWORD # 24 - CURLUE_BAD_PATH # 25 - CURLUE_BAD_QUERY # 26 - CURLUE_BAD_SCHEME # 27 - CURLUE_BAD_SLASHES # 28 - CURLUE_BAD_USER # 29 - CURLUE_LACKS_IDN # 30 - CURLUE_LAST - - CURLUPart* {.size: sizeof(cint).} = enum - CURLUPART_URL - CURLUPART_SCHEME - CURLUPART_USER - CURLUPART_PASSWORD - CURLUPART_OPTIONS - CURLUPART_HOST - CURLUPART_PORT - CURLUPART_PATH - CURLUPART_QUERY - CURLUPART_FRAGMENT - CURLUPART_ZONEID # added in 7.65.0 - - curl_header* = object - name*: cstring - value*: cstring - amount*: csize_t - index*: csize_t - origin*: cint - anchor*: pointer - -proc `==`*(a: CURL; b: CURL): bool {.borrow.} -proc `==`*(a: CURL; b: typeof(nil)): bool {.borrow.} -proc `==`*(a: CURLM; b: CURLM): bool {.borrow.} -proc `==`*(a: CURLM; b: typeof(nil)): bool {.borrow.} - -{.push importc.} - -proc curl_global_init*(flags: clong): CURLcode -proc curl_global_cleanup*() -proc curl_free*(p: pointer) - -proc curl_easy_init*(): CURL -proc curl_easy_cleanup*(handle: CURL) -proc curl_easy_setopt*(handle: CURL; option: CURLoption): CURLcode {.varargs.} -proc curl_easy_perform*(handle: CURL): CURLcode -proc curl_easy_getinfo*(handle: CURL; info: CURLINFO): CURLcode {.varargs.} -proc curl_easy_strerror*(errornum: CURLcode): cstring -proc curl_easy_nextheader*(curl: CURL; origin: cuint; request: cint; - prev: ptr curl_header): ptr curl_header -proc curl_easy_header*(curl: CURL; name: cstring; index: csize_t; origin: cuint; - request: cint; hout: out ptr curl_header): cint - -proc curl_url*(): CURLU -proc curl_url_cleanup*(handle: CURLU) -proc curl_url_dup*(inh: CURLU): CURLU -proc curl_url_get*(handle: CURLU; what: CURLUPart; part: ptr cstring; - flags: cuint): CURLUcode -proc curl_url_set*(handle: CURLU; what: CURLUPart; part: cstring; - flags: cuint): CURLUcode -proc curl_url_strerror*(code: CURLUcode): cstring - -proc curl_mime_init*(handle: CURL): curl_mime -proc curl_mime_free*(mime: curl_mime) -proc curl_mime_addpart*(mime: curl_mime): curl_mimepart -proc curl_mime_name*(part: curl_mimepart; name: cstring) -proc curl_mime_data*(part: curl_mimepart; data: pointer; datasize: csize_t) -proc curl_mime_filename*(part: curl_mimepart; name: cstring) -proc curl_mime_filedata*(part: curl_mimepart; filename: cstring) - -proc curl_slist_append*(slist: curl_slist; str: cstring): curl_slist -proc curl_slist_free_all*(slist: curl_slist) - -proc curl_multi_init*(): CURLM -proc curl_multi_add_handle*(multi_handle: CURLM; curl_handle: CURL): CURLMcode -proc curl_multi_remove_handle*(multi_handle: CURLM; curl_handle: CURL): CURLMcode -proc curl_multi_fdset*(multi_handle: CURLM; read_fd_set, write_fd_set, - exc_fd_set: pointer; max_fd: ptr cint): CURLMcode -proc curl_multi_wait*(multi_handle: CURLM; extra_fds: ptr curl_waitfd; - extra_nfds: cuint; timeout_ns: cint; ret: ptr cint): CURLMcode -proc curl_multi_poll*(multi_handle: CURLM; extra_fds: ptr curl_waitfd; - extra_nfds: cuint; timeout_ns: cint; ret: ptr cint): CURLMcode -proc curl_multi_wakeup*(multi_handle: CURLM): CURLMcode -proc curl_multi_perform*(multi_handle: CURLM; running_handles: ptr cint): - CURLMcode -proc curl_multi_cleanup*(multi_handle: CURLM): CURLMcode -proc curl_multi_info_read*(multi_handle: CURLM; msgs_in_queue: ptr cint): CURLMsg -proc curl_multi_strerror*(code: CURLMcode): cstring -{.pop.} - -{.pop.} - - -template setopt(curl: CURL; opt: CURLoption; arg: typed) = - discard curl_easy_setopt(curl, opt, arg) - -template setopt(curl: CURL; opt: CURLoption; arg: string) = - discard curl_easy_setopt(curl, opt, cstring(arg)) - -template getinfo(curl: CURL; info: CURLINFO; arg: typed) = - discard curl_easy_getinfo(curl, info, arg) - -template set(url: CURLU; part: CURLUPart; content: cstring; flags: cuint) = - discard curl_url_set(url, part, content, flags) - -template set(url: CURLU; part: CURLUPart; content: string; flags: cuint) = - url.set(part, cstring(content), flags) - -func curlErrorToChaError(res: CURLcode): string = - return case res - of CURLE_OK: "" - of CURLE_URL_MALFORMAT: "InvalidURL" #TODO should never occur... - of CURLE_COULDNT_CONNECT: "ConnectionRefused" - of CURLE_COULDNT_RESOLVE_PROXY: "FailedToResolveProxy" - of CURLE_COULDNT_RESOLVE_HOST: "FailedToResolveHost" - of CURLE_PROXY: "ProxyRefusedToConnect" - else: "InternalError" - -proc getCurlConnectionError(res: CURLcode): string = - let e = curlErrorToChaError(res) - let msg = $curl_easy_strerror(res) - return "Cha-Control: ConnectionError " & e & " " & msg & "\n" + TINFL_FLAG_PARSE_ZLIB_HEADER = 0x01u32 + TINFL_FLAG_PARSE_GZIP_HEADER = 0x02u32 + TINFL_FLAG_HAS_MORE_INPUT = 0x04u32 + TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF* = 0x08u32 + +type tinfl_status {.size: sizeof(cint).} = enum + TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS = -5 + TINFL_STATUS_BAD_PARAM = -4 + TINFL_STATUS_ISIZE_OR_CRC32_MISMATCH = -3 + TINFL_STATUS_ADLER32_MISMATCH = -2 + TINFL_STATUS_FAILED = -1 + TINFL_STATUS_DONE = 0 + TINFL_STATUS_NEEDS_MORE_INPUT = 1 + TINFL_STATUS_HAS_MORE_OUTPUT = 2 + +type tinfl_huff_table {.importc, header: "tinfl.h", completeStruct.} = object + m_code_size: array[TINFL_MAX_HUFF_SYMBOLS_0, uint8] + m_look_up: array[TINFL_FAST_LOOKUP_SIZE, uint16] + m_tree: array[TINFL_MAX_HUFF_SYMBOLS_0 * 2, uint16] + +type tinfl_decompressor {.importc, header: "tinfl.h", completeStruct.} = object + m_state, m_num_bits, m_zhdr0, m_zhdr1, m_g_isize: uint32 + m_checksum, m_checksum_current: uint32 + m_final, m_type, m_check_adler32, m_dist, m_counter, m_num_extra: uint32 + m_table_sizes: array[TINFL_MAX_HUFF_TABLES, uint32] + + m_bit_buf: uint64 + m_dist_from_out_buf_start: csize_t + m_tables: array[TINFL_MAX_HUFF_TABLES, tinfl_huff_table] + + m_raw_header: array[4, uint8] + m_len_codes: array[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137, + uint8] + m_gz_header: array[10, uint8] + +{.push importc, cdecl, header: """ +#define TINFL_IMPLEMENTATION +#include "tinfl.h" +""".} +proc tinfl_decompress(r: var tinfl_decompressor; pIn_buf_next: ptr uint8; + pIn_buf_size: var csize_t; pOut_buf_start, pOut_buf_next: ptr uint8; + pOut_buf_size: var csize_t; decomp_flags: uint32): tinfl_status +{.pop.} # importc, cdecl, header: "tinfl.h" + +const InputBufferSize = 16384 type - EarlyHintState = enum - ehsNone, ehsStarted, ehsDone - - HttpHandle = ref object - curl: CURL - statusline: bool - connectreport: bool - earlyhint: EarlyHintState - slist: curl_slist - -const STDIN_FILENO = 0 -const STDOUT_FILENO = 1 - -proc writeAll(data: pointer; size: int) = - var n = 0 - while n < size: - let i = write(STDOUT_FILENO, addr cast[ptr UncheckedArray[uint8]](data)[n], - int(size) - n) - assert i >= 0 - n += i - -proc puts(s: string) = - if s.len > 0: - writeAll(unsafeAddr s[0], s.len) - -proc curlWriteHeader(p: cstring; size, nitems: csize_t; userdata: pointer): - csize_t {.cdecl.} = - var line = newString(nitems) - if nitems > 0: - copyMem(addr line[0], p, nitems) - let op = cast[HttpHandle](userdata) - if not op.statusline: - op.statusline = true - var status: clong - op.curl.getinfo(CURLINFO_RESPONSE_CODE, addr status) - if status == 103: - op.earlyhint = ehsStarted - else: - op.connectreport = true - puts("Status: " & $status & "\nCha-Control: ControlDone\n") - return nitems - if line == "\r\n" or line == "\n": - # empty line (last, before body) - if op.earlyhint == ehsStarted: - # ignore; we do not have a way to stream headers yet. - op.earlyhint = ehsDone - # reset statusline; we are awaiting the next line. - op.statusline = false - return nitems - puts("\r\n") - return nitems - - if op.earlyhint != ehsStarted: - # Regrettably, we can only write early hint headers after the status - # code is already known. - # For now, it seems easiest to just ignore them all. - puts(line) - return nitems - -# From the documentation: size is always 1. -proc curlWriteBody(p: cstring; size, nmemb: csize_t; userdata: pointer): - csize_t {.cdecl.} = - return csize_t(write(STDOUT_FILENO, p, int(nmemb))) - -# From the documentation: size is always 1. -proc readFromStdin(p: pointer; size, nitems: csize_t; userdata: pointer): - csize_t {.cdecl.} = - return csize_t(read(STDIN_FILENO, p, int(nitems))) - -proc curlPreRequest(clientp: pointer; conn_primary_ip, conn_local_ip: cstring; - conn_primary_port, conn_local_port: cint): cint {.cdecl.} = - let op = cast[HttpHandle](clientp) - op.connectreport = true - puts("Cha-Control: Connected\n") - enterNetworkSandbox() - return 0 # ok - -func startsWithIgnoreCase(s1, s2: openArray[char]): bool = - if s1.len < s2.len: return false - for i in 0 ..< s2.len: - if s1[i].toLowerAscii() != s2[i].toLowerAscii(): - return false - return true + HTTPHandle = ref object + state: HTTPState + bodyState: HTTPState # if TE is chunked, hsChunkSize; else hsBody + lineState: LineState + chunkSize: uint64 # Content-Length if TE is not chunked + ps: DynStream + os: PosixStream + line: string + headers: seq[tuple[key, value: string]] + + LineState = enum + lsNone, lsCrSeen + + HTTPState = enum + hsStatus, hsHeaders, hsChunkSize, hsAfterChunk, hsBody, hsTrailers, hsDone + + ContentEncoding = enum + ceGzip = "gzip" + ceDeflate = "deflate" + + TransferEncoding = enum + teChunked = "chunked" + teGzip = "gzip" + teDeflate = "deflate" + +proc inflate(op: HTTPHandle; flag: uint32) = + var pipefd {.noinit.}: array[2, cint] + if pipe(pipefd) != 0: + return + let pins = newPosixStream(pipefd[0]) + let pouts = newPosixStream(pipefd[1]) + case fork() + of -1: + pins.sclose() + pouts.sclose() + of 0: # child + enterNetworkSandbox() + pouts.sclose() + let os = op.os + var flags = flag or TINFL_FLAG_HAS_MORE_INPUT + var decomp = tinfl_decompressor() + var iq {.noinit.}: array[InputBufferSize, uint8] + var oq {.noinit.}: array[TINFL_LZ_DICT_SIZE, uint8] + var oqoff = 0 + while true: + let len0 = pins.readData(iq) + if len0 <= 0: + break + let len = csize_t(len0) + var n = csize_t(0) + while n < len: + var iqn = csize_t(len) - n + var oqn = csize_t(oq.len - oqoff) + let status = decomp.tinfl_decompress(addr iq[n], iqn, addr oq[0], + addr oq[oqoff], oqn, flags) + if not os.writeDataLoop(oq.toOpenArray(oqoff, oqoff + int(oqn) - 1)): + quit(1) + oqoff = int((csize_t(oqoff) + oqn) and csize_t(oq.len) - 1) + n += iqn + case status + of TINFL_STATUS_HAS_MORE_OUTPUT: + discard + of TINFL_STATUS_NEEDS_MORE_INPUT: + assert len == n + of TINFL_STATUS_DONE: + quit(0) + of TINFL_STATUS_BAD_PARAM: assert false + of TINFL_STATUS_ADLER32_MISMATCH, TINFL_STATUS_FAILED, + TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS, + TINFL_STATUS_ISIZE_OR_CRC32_MISMATCH: + stderr.writeLine("NewHTTP error: " & $status) + quit(1) + quit(0) + else: # parent + pins.sclose() + op.os = pouts + +proc handleStatus(op: HTTPHandle; iq: openArray[char]): int = + for i, c in iq: + case op.lineState + of lsNone: + if c == '\r': + op.lineState = lsCrSeen + else: + op.line &= c + of lsCrSeen: + if c != '\n' or + not op.line.startsWithIgnoreCase("HTTP/1.1") and + not op.line.startsWithIgnoreCase("HTTP/1.0"): + quit(1) + let codes = op.line.until(' ', "HTTP/1.0 ".len) + let code = parseUInt16(codes) + if codes.len > 3 or code.isNone: + quit(1) + let buf = "Status: " & $code.get & "\r\nCha-Control: ControlDone\r\n" + if not op.os.writeDataLoop(buf): + quit(1) + op.lineState = lsNone + op.state = hsHeaders + op.line = "" + return i + 1 + return iq.len + +proc handleHeaders(op: HTTPHandle; iq: openArray[char]): int = + for i, c in iq: + case op.lineState + of lsNone: + if c == '\r': + op.lineState = lsCrSeen + else: + op.line &= c + of lsCrSeen: + if c != '\n': # malformed header + quit(1) + if op.line != "": + var name = op.line.until(':') + if name.len > 0 and name[0] notin HTTPWhitespace and + name.len != op.line.len: + name = name.strip(leading = false, trailing = true, + chars = HTTPWhitespace) + let value = op.line.after(':').strip(leading = true, + trailing = false, chars = HTTPWhitespace) + op.headers.add((move(name), value)) + op.line = "" + op.lineState = lsNone + else: + var buf = "" + var contentEncodings: seq[ContentEncoding] = @[] + var transferEncodings: seq[TransferEncoding] = @[] + var contentLength = uint64.high + for it in op.headers: + buf &= it.key & ": " & it.value & "\r\n" + if it.key.equalsIgnoreCase("Content-Encoding"): + for it in it.value.split(','): + let x = parseEnumNoCase[ContentEncoding](it) + if x.isSome: + contentEncodings.add(x.get) + elif it.key.equalsIgnoreCase("Transfer-Encoding"): + for it in it.value.split(','): + let x = parseEnumNoCase[TransferEncoding](it) + if x.isSome: + transferEncodings.add(x.get) + elif it.key.equalsIgnoreCase("Content-Length"): + contentLength = parseUInt64(it.value).get(uint64.high) + buf &= "\r\n" + if not op.os.writeDataLoop(buf): + quit(1) + for i in countdown(contentEncodings.high, 0): + case contentEncodings[i] + of ceGzip: op.inflate(TINFL_FLAG_PARSE_GZIP_HEADER) + of ceDeflate: op.inflate(TINFL_FLAG_PARSE_ZLIB_HEADER) + op.bodyState = hsBody + for i in countdown(transferEncodings.high, 0): + case transferEncodings[i] + of teChunked: + if i == 0: + op.bodyState = hsChunkSize + of teGzip: op.inflate(TINFL_FLAG_PARSE_GZIP_HEADER) + of teDeflate: op.inflate(TINFL_FLAG_PARSE_ZLIB_HEADER) + op.lineState = lsNone + op.state = op.bodyState + if op.bodyState == hsBody: + op.chunkSize = contentLength + return i + 1 + return iq.len + +proc handleChunkSize(op: HTTPHandle; iq: openArray[char]): int = + for i, c in iq: + case op.lineState + of lsNone: + if c == '\r': + op.lineState = lsCrSeen + else: + let n = hexValue(c) + let osize = op.chunkSize + op.chunkSize = osize * 0x10 + uint64(n) + if n == -1 or osize > op.chunkSize: + stderr.writeLine("NewHTTP error: error decoding chunk size") + quit(1) + of lsCrSeen: + if c != '\n': + stderr.writeLine("NewHTTP error: CRLF expected") + quit(1) + op.lineState = lsNone + if op.chunkSize > 0: + op.state = hsBody + return i + 1 + op.state = hsTrailers + break + return iq.len + +proc handleBody(op: HTTPHandle; iq: openArray[char]): int = + var L = uint64(iq.len) + if L >= op.chunkSize: + L = op.chunkSize + op.state = hsAfterChunk + let n = int(L) + if not op.os.writeDataLoop(iq.toOpenArray(0, n - 1)): + quit(1) + op.chunkSize -= L + if op.bodyState == hsBody and op.chunkSize == 0: + return -1 # we're done + return n + +proc handleAfterChunk(op: HTTPHandle; iq: openArray[char]): int = + for i, c in iq: + case op.lineState + of lsNone: + if c != '\r': + quit(1) + op.lineState = lsCrSeen + of lsCrSeen: + if c != '\n': + quit(1) + op.lineState = lsNone + op.state = hsChunkSize + return i + 1 + return iq.len + +proc handleTrailers(op: HTTPHandle; iq: openArray[char]): int = + for i, c in iq: + case op.lineState + of lsNone: + if c == '\r': + op.lineState = lsCrSeen + else: + op.line &= c + of lsCrSeen: + if c != '\n': + quit(1) + op.lineState = lsNone + if op.line == "": + op.state = hsDone + return i + 1 + op.line = "" + return iq.len + +proc handleBuffer(op: HTTPHandle; iq: openArray[char]): int = + case op.state + of hsStatus: return op.handleStatus(iq) + of hsHeaders: return op.handleHeaders(iq) + of hsChunkSize: return op.handleChunkSize(iq) + of hsBody: return op.handleBody(iq) + of hsAfterChunk: return op.handleAfterChunk(iq) # CRLF after a chunk + of hsTrailers: return op.handleTrailers(iq) + of hsDone: return -1 + +proc checkCert(os: PosixStream; ssl: ptr SSL) = + let res = SSL_get_verify_result(ssl) + if res != X509_V_OK: + let s = X509_verify_cert_error_string(res) + os.die("InvalidResponse", $s) proc main() = - let curl = curl_easy_init() - doAssert curl != nil - let url = curl_url() - const flags = cuint(CURLU_PATH_AS_IS) - url.set(CURLUPART_SCHEME, getEnv("MAPPED_URI_SCHEME"), flags) + let secure = getEnv("MAPPED_URI_SCHEME") == "https" let username = getEnv("MAPPED_URI_USERNAME") - if username != "": - url.set(CURLUPART_USER, username, flags) let password = getEnv("MAPPED_URI_PASSWORD") - if password != "": - url.set(CURLUPART_PASSWORD, password, flags) - url.set(CURLUPART_HOST, getEnv("MAPPED_URI_HOST"), flags) - let port = getEnv("MAPPED_URI_PORT") - if port != "": - url.set(CURLUPART_PORT, port, flags) - let path = getEnv("MAPPED_URI_PATH") - if path != "": - url.set(CURLUPART_PATH, path, flags) + let host = getEnv("MAPPED_URI_HOST") + let port = getEnvEmpty("MAPPED_URI_PORT", if secure: "443" else: "80") + let path = getEnvEmpty("MAPPED_URI_PATH", "/") let query = getEnv("MAPPED_URI_QUERY") + let os = newPosixStream(STDOUT_FILENO) + let ps = if secure: + let ssl = os.connectSSLSocket(host, port, useDefaultCA = true) + if getEnv("CHA_INSECURE_SSL_NO_VERIFY") != "1": + os.checkCert(ssl) + newSSLStream(ssl) + else: + os.connectSocket(host, port) + let op = HTTPHandle(ps: ps, os: os) + let requestMethod = getEnv("REQUEST_METHOD") + var buf = requestMethod & ' ' & path if query != "": - url.set(CURLUPART_QUERY, query, flags) - if getEnv("CHA_INSECURE_SSL_NO_VERIFY") == "1": - curl.setopt(CURLOPT_SSL_VERIFYPEER, 0) - curl.setopt(CURLOPT_SSL_VERIFYHOST, 0) - curl.setopt(CURLOPT_CURLU, url) - let op = HttpHandle(curl: curl) - curl.setopt(CURLOPT_SUPPRESS_CONNECT_HEADERS, 1) - curl.setopt(CURLOPT_WRITEFUNCTION, curlWriteBody) - curl.setopt(CURLOPT_HEADERDATA, op) - curl.setopt(CURLOPT_HEADERFUNCTION, curlWriteHeader) - curl.setopt(CURLOPT_PREREQDATA, op) - curl.setopt(CURLOPT_PREREQFUNCTION, curlPreRequest) - curl.setopt(CURLOPT_NOSIGNAL, 1) - let proxy = getEnv("ALL_PROXY") - if proxy != "": - curl.setopt(CURLOPT_PROXY, proxy) - case getEnv("REQUEST_METHOD") - of "GET": - curl.setopt(CURLOPT_HTTPGET, 1) - of "POST": - curl.setopt(CURLOPT_POST, 1) - let len = parseInt(getEnv("CONTENT_LENGTH")) - # > For any given platform/compiler curl_off_t must be typedef'ed to - # a 64-bit - # > wide signed integral data type. The width of this data type must remain - # > constant and independent of any possible large file support settings. - # > - # > As an exception to the above, curl_off_t shall be typedef'ed to - # a 32-bit - # > wide signed integral data type if there is no 64-bit type. - # It seems safe to assume that if the platform has no uint64 then Nim won't - # compile either. In return, we are allowed to post >2G of data. - curl.setopt(CURLOPT_POSTFIELDSIZE_LARGE, uint64(len)) - curl.setopt(CURLOPT_READFUNCTION, readFromStdin) - else: discard #TODO - let headers = getEnv("REQUEST_HEADERS") - for line in headers.split("\r\n"): - const needle = "Accept-Encoding: " - if line.startsWithIgnoreCase(needle): - let s = line.substr(needle.len) - # From the CURLOPT_ACCEPT_ENCODING manpage: - # > The application does not have to keep the string around after - # > setting this option. - curl.setopt(CURLOPT_ACCEPT_ENCODING, cstring(s)) - # This is OK, because curl_slist_append strdup's line. - op.slist = curl_slist_append(op.slist, cstring(line)) - if op.slist != nil: - curl.setopt(CURLOPT_HTTPHEADER, op.slist) - let res = curl_easy_perform(curl) - if res != CURLE_OK and not op.connectreport: - puts(getCurlConnectionError(res)) - op.connectreport = true - curl_easy_cleanup(curl) + buf &= '?' & query + buf &= " HTTP/1.1\r\n" + buf &= "Host: " & host + if secure and port != "443" or not secure and port != "80": + buf &= ':' & port + buf &= "\r\n" + buf &= "Connection: close\r\n" + if username != "": + buf &= "Authorization: Basic " & btoa(username & ':' & password) & "\r\n" + let contentLength = getEnv("CONTENT_LENGTH") + if (let x = parseUInt64(contentLength); x.isSome): + buf &= "Content-Length: " & $x.get & "\r\n" + buf &= getEnv("REQUEST_HEADERS") + buf &= "\r\n" + if not op.ps.writeDataLoop(buf): + os.die("ConnectionRefused", "error sending request header") + var iq {.noinit.}: array[InputBufferSize, char] + if requestMethod == "POST": + let ps = newPosixStream(STDIN_FILENO) + while (let n = ps.readData(iq); n > 0): + if not op.ps.writeDataLoop(iq.toOpenArray(0, n - 1)): + os.die("ConnectionRefused", "error sending request body") + if not os.writeDataLoop("Cha-Control: Connected\r\n"): + quit(1) + block readResponse: + while (let n = ps.readData(iq); n > 0): + var m = 0 + while m < n: + let k = op.handleBuffer(iq.toOpenArray(m, n - 1)) + if k == -1: # hsDone + break readResponse + m += k + op.ps.sclose() main() diff --git a/bonus/newhttp/tinfl.h b/adapter/protocol/tinfl.h index a05c547c..a05c547c 100644 --- a/bonus/newhttp/tinfl.h +++ b/adapter/protocol/tinfl.h diff --git a/bonus/Makefile b/bonus/Makefile index 1a7b0b1d..0b5f34c9 100644 --- a/bonus/Makefile +++ b/bonus/Makefile @@ -48,12 +48,13 @@ install-magnet.cgi: magnet.cgi cp magnet.cgi $(CHA_CGI_DIR) ./addurimethod magnet magnet.cgi -.PHONY: install-newhttp -install-newhttp: +.PHONY: install-curlhttp +install-curlhttp: mkdir -p $(CHA_CGI_DIR) - nim c -f -d:release -d:lto -o:$(CHA_CGI_DIR)/newhttp newhttp/http.nim - ./addurimethod http newhttp - ./addurimethod https newhttp + nim c -f -d:release -d:lto -d:curlLibName=$(CURLLIBNAME) \ + -o:$(CHA_CGI_DIR)/curlhttp curlhttp.nim + ./addurimethod http curlhttp + ./addurimethod https curlhttp .PHONY: install-trans.cgi install-trans.cgi: trans.cgi diff --git a/bonus/README.md b/bonus/README.md index be8b2cbb..065d61d4 100644 --- a/bonus/README.md +++ b/bonus/README.md @@ -22,6 +22,12 @@ Additional documentation is embedded at the beginning of each file. Please read it. (Note that the Makefile automates the installation instructions, so you can skip those.) +### [curlhttp](curlhttp/) + +Old HTTP(S) handler based on libcurl. This is mainly useful if you want +to use curl-impersonate; in this case, install it like +`make install-libcurl CURLLILBNAME=libcurl-impersonate-chrome.so`. + ### [filei.cgi](filei.cgi) Album view of a directory. Requires `buffer.images = true`. @@ -44,18 +50,6 @@ designed to handle file downloads, not web browsing. A `magnet:` URL handler. It can forward magnet links to transmission. -### [newhttp](newhttp/) - -WIP new HTTP(S) CGI handler. For now, the only difference between -this and the current one is that this uses tinfl for decompression. - -Still TODO: - -* somehow merge TOFU logic/interface with gemini, sftp -* keep-alive (loader needs work first) -* HTTP/2 -* sandbox - ### [stbir2](stbir2/) By default, Chawan uses stb_image_resize for resizing images, but there diff --git a/bonus/curlhttp.nim b/bonus/curlhttp.nim new file mode 100644 index 00000000..fd647075 --- /dev/null +++ b/bonus/curlhttp.nim @@ -0,0 +1,655 @@ +when NimMajor >= 2: + import std/envvars +else: + import std/os +import std/posix +import std/strutils +import utils/sandbox + +const curllib = (func(): string = + const curlLibName {.strdefine.} = "" + when curlLibName != "": + return curlLibName + elif defined(macosx): + return "libcurl(|.4|.4.8.0).dylib" + else: # assume elf + return "libcurl.so(|.4|.4.8.0)" +)() + +const + CURL_GLOBAL_SSL* = 1 shl 0 # no purpose since 7.57.0 + CURL_GLOBAL_WIN32* = 1 shl 1 + CURL_GLOBAL_ALL* = CURL_GLOBAL_SSL or CURL_GLOBAL_WIN32 + CURL_GLOBAL_NOTHING* = 0 + CURL_GLOBAL_DEFAULT* = CURL_GLOBAL_ALL + CURL_GLOBAL_ACK_EINTR* = 1 shl 2 + +const + CURLOPTTYPE_LONG = 0 + CURLOPTTYPE_OBJECTPOINT = 10000 + CURLOPTTYPE_FUNCTIONPOINT = 20000 + CURLOPTTYPE_OFF_T = 30000 + CURLOPTTYPE_BLOB = 40000 + +const + CURLOPTTYPE_STRINGPOINT = CURLOPTTYPE_OBJECTPOINT + CURLOPTTYPE_SLISTPOINT = CURLOPTTYPE_OBJECTPOINT + CURLOPTTYPE_CBPOINT = CURLOPTTYPE_OBJECTPOINT + CURLOPTTYPE_VALUES = CURLOPTTYPE_LONG + +const + CURLINFO_STRING = 0x100000 + CURLINFO_LONG = 0x200000 + CURLINFO_DOUBLE = 0x300000 + CURLINFO_SLIST = 0x400000 + CURLINFO_PTR = 0x400000 # same as SLIST + CURLINFO_SOCKET = 0x500000 + CURLINFO_OFF_T = 0x600000 + CURLINFO_MASK {.used.} = 0x0fffff + CURLINFO_TYPEMASK {.used.} = 0xf00000 + +const + CURL_WAIT_POLLIN* = 0x0001 + CURL_WAIT_POLLPRI* = 0x0002 + CURL_WAIT_POLLOUT* = 0x0004 + +# CURLU +const + CURLU_DEFAULT_PORT* = (1 shl 0) # return default port number + CURLU_NO_DEFAULT_PORT* = (1 shl 1) # act as if no port number was set, + # if the port number matches the + # default for the scheme + CURLU_DEFAULT_SCHEME* = (1 shl 2) # return default scheme if missing + CURLU_NON_SUPPORT_SCHEME* = (1 shl 3) # allow non-supported scheme + CURLU_PATH_AS_IS* = (1 shl 4) # leave dot sequences + CURLU_DISALLOW_USER* = (1 shl 5) # no user+password allowed + CURLU_URLDECODE* = (1 shl 6) # URL decode on get + CURLU_URLENCODE* = (1 shl 7) # URL encode on set + CURLU_APPENDQUERY* = (1 shl 8) # append a form style part + CURLU_GUESS_SCHEME* = (1 shl 9) # legacy curl-style guessing + CURLU_NO_AUTHORITY* = (1 shl 10) # Allow empty authority when the scheme + # is unknown. + CURLU_ALLOW_SPACE* = (1 shl 11) # Allow spaces in the URL + CURLU_PUNYCODE* = (1 shl 12) # get the host name in punycode + CURLU_PUNY2IDN* = (1 shl 13) # punycode => IDN conversion + +const + CURLH_HEADER* = 1 shl 0 + CURLH_TRAILER* = 1 shl 1 + CURLH_CONNECT* = 1 shl 2 + CURLH_1XX* = 1 shl 3 + CURLH_PSEUDO* = 1 shl 4 + +{.push cdecl, dynlib: curllib.} + +type + CURL* = distinct pointer + CURLM* = distinct pointer + CURLU* = distinct pointer + + curl_mime_struct = object + curl_mime* = ptr curl_mime_struct + curl_mimepart_struct = object + curl_mimepart* = ptr curl_mimepart_struct + curl_slist_struct = object + curl_slist* = ptr curl_slist_struct + curl_socket_t = cint + curl_waitfd* = object + fd*: curl_socket_t + events*: cshort + revents*: cshort # this is, in fact, supported. + CURLMsg_data {.union.} = object + whatever: pointer + result*: CURLcode + CURLMsg_struct = object + msg*: CURLMSG_E + easy_handle*: CURL + data*: CURLMsg_data + CURLMsg* = ptr CURLMsg_struct + + CURLoption* {.size: sizeof(cint).} = enum + # Long + CURLOPT_PORT = CURLOPTTYPE_LONG + 3 + CURLOPT_SSLVERSION = CURLOPTTYPE_VALUES + 32 + CURLOPT_TIMECONDITION = CURLOPTTYPE_VALUES + 33 + CURLOPT_POST = CURLOPTTYPE_LONG + 47 + CURLOPT_DIRLISTONLY = CURLOPTTYPE_LONG + 48 + CURLOPT_FOLLOWLOCATION = CURLOPTTYPE_LONG + 52 + CURLOPT_POSTFIELDSIZE = CURLOPTTYPE_LONG + 60 + CURLOPT_SSL_VERIFYPEER = CURLOPTTYPE_LONG + 64 + CURLOPT_HTTPGET = CURLOPTTYPE_LONG + 80 + CURLOPT_SSL_VERIFYHOST = CURLOPTTYPE_LONG + 81 + CURLOPT_NOSIGNAL = CURLOPTTYPE_LONG + 99 + CURLOPT_FTP_FILEMETHOD = CURLOPTTYPE_VALUES + 138 + CURLOPT_CONNECT_ONLY = CURLOPTTYPE_LONG + 141 + CURLOPT_SUPPRESS_CONNECT_HEADERS = CURLOPTTYPE_LONG + 265 + + # Objectpoint + CURLOPT_WRITEDATA = CURLOPTTYPE_CBPOINT + 1 + CURLOPT_URL = CURLOPTTYPE_STRINGPOINT + 2 + CURLOPT_PROXY = CURLOPTTYPE_STRINGPOINT + 4 + CURLOPT_ERRORBUFFER = CURLOPTTYPE_OBJECTPOINT + 10 + CURLOPT_POSTFIELDS = CURLOPTTYPE_OBJECTPOINT + 15 + CURLOPT_HTTPHEADER = CURLOPTTYPE_SLISTPOINT + 23 + CURLOPT_KEYPASSWD = CURLOPTTYPE_STRINGPOINT + 26 + CURLOPT_HEADERDATA = CURLOPTTYPE_CBPOINT + 29 + CURLOPT_ACCEPT_ENCODING = CURLOPTTYPE_STRINGPOINT + 102 + CURLOPT_SSH_PUBLIC_KEYFILE = CURLOPTTYPE_STRINGPOINT + 152 + CURLOPT_SSH_PRIVATE_KEYFILE = CURLOPTTYPE_STRINGPOINT + 153 + CURLOPT_MIMEPOST = CURLOPTTYPE_OBJECTPOINT + 269 + CURLOPT_CURLU = CURLOPTTYPE_OBJECTPOINT + 282 + CURLOPT_PREREQDATA = CURLOPTTYPE_CBPOINT + 313 + + # Functionpoint + CURLOPT_WRITEFUNCTION = CURLOPTTYPE_FUNCTIONPOINT + 11 + CURLOPT_READFUNCTION = CURLOPTTYPE_FUNCTIONPOINT + 12 + CURLOPT_HEADERFUNCTION = CURLOPTTYPE_FUNCTIONPOINT + 79 + CURLOPT_PREREQFUNCTION = CURLOPTTYPE_FUNCTIONPOINT + 312 + + # Off-t + CURLOPT_INFILESIZE_LARGE = CURLOPTTYPE_OFF_T + 115 + CURLOPT_RESUME_FROM_LARGE = CURLOPTTYPE_OFF_T + 116 + CURLOPT_POSTFIELDSIZE_LARGE = CURLOPTTYPE_OFF_T + 120 + + # Blob + CURLOPT_SSLCERT_BLOB = CURLOPTTYPE_BLOB + 291 + CURLOPT_SSLKEY_BLOB = CURLOPTTYPE_BLOB + 292 + CURLOPT_PROXY_SSLCERT_BLOB = CURLOPTTYPE_BLOB + 293 + CURLOPT_PROXY_SSLKEY_BLOB = CURLOPTTYPE_BLOB + 294 + CURLOPT_ISSUECERT_BLOB = CURLOPTTYPE_BLOB + 295 + + CURLINFO* {.size: sizeof(cint).} = enum + CURLINFO_NONE # first, never use this + + # String + CURLINFO_REDIRECT_URL = CURLINFO_STRING + 31 + + # Long + CURLINFO_RESPONSE_CODE = CURLINFO_LONG + 2 + + # Double + CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE + 3 + + # S-list + CURLINFO_SSL_ENGINES = CURLINFO_SLIST + 27 + CURLINFO_COOKIELIST = CURLINFO_SLIST + 28 + + # Pointer + CURLINFO_CERTINFO = CURLINFO_PTR + 34 + CURLINFO_TLS_SESSION = CURLINFO_PTR + 43 + CURLINFO_TLS_SSL_PTR = CURLINFO_PTR + 45 + + # Socket + CURLINFO_ACTIVESOCKET = CURLINFO_SOCKET + 44 + + # Off_t + CURLINFO_SIZE_UPLOAD_T = CURLINFO_OFF_T + 7 + CURLINFO_SIZE_DOWNLOAD_T = CURLINFO_OFF_T + 9 + + CURLcode* {.size: sizeof(cint).} = enum + CURLE_OK = 0, + CURLE_UNSUPPORTED_PROTOCOL, # 1 + CURLE_FAILED_INIT, # 2 + CURLE_URL_MALFORMAT, # 3 + CURLE_NOT_BUILT_IN, # 4 - [was obsoleted in August 2007 for + # 7.17.0, reused in April 2011 for 7.21.5] + CURLE_COULDNT_RESOLVE_PROXY, # 5 + CURLE_COULDNT_RESOLVE_HOST, # 6 + CURLE_COULDNT_CONNECT, # 7 + CURLE_WEIRD_SERVER_REPLY, # 8 + CURLE_REMOTE_ACCESS_DENIED, # 9 a service was denied by the server + # due to lack of access - when login fails + # this is not returned. + CURLE_FTP_ACCEPT_FAILED, # 10 - [was obsoleted in April 2006 for + # 7.15.4, reused in Dec 2011 for 7.24.0] + CURLE_FTP_WEIRD_PASS_REPLY, # 11 + CURLE_FTP_ACCEPT_TIMEOUT, # 12 - timeout occurred accepting server + # [was obsoleted in August 2007 for 7.17.0, + # reused in Dec 2011 for 7.24.0] + CURLE_FTP_WEIRD_PASV_REPLY, # 13 + CURLE_FTP_WEIRD_227_FORMAT, # 14 + CURLE_FTP_CANT_GET_HOST, # 15 + CURLE_HTTP2, # 16 - A problem in the http2 framing layer. + # [was obsoleted in August 2007 for 7.17.0, + # reused in July 2014 for 7.38.0] + CURLE_FTP_COULDNT_SET_TYPE, # 17 + CURLE_PARTIAL_FILE, # 18 + CURLE_FTP_COULDNT_RETR_FILE, # 19 + CURLE_OBSOLETE20, # 20 - NOT USED + CURLE_QUOTE_ERROR, # 21 - quote command failure + CURLE_HTTP_RETURNED_ERROR, # 22 + CURLE_WRITE_ERROR, # 23 + CURLE_OBSOLETE24, # 24 - NOT USED + CURLE_UPLOAD_FAILED, # 25 - failed upload "command" + CURLE_READ_ERROR, # 26 - couldn't open/read from file + CURLE_OUT_OF_MEMORY, # 27 + CURLE_OPERATION_TIMEDOUT, # 28 - the timeout time was reached + CURLE_OBSOLETE29, # 29 - NOT USED + CURLE_FTP_PORT_FAILED, # 30 - FTP PORT operation failed + CURLE_FTP_COULDNT_USE_REST, # 31 - the REST command failed + CURLE_OBSOLETE32, # 32 - NOT USED + CURLE_RANGE_ERROR, # 33 - RANGE "command" didn't work + CURLE_HTTP_POST_ERROR, # 34 + CURLE_SSL_CONNECT_ERROR, # 35 - wrong when connecting with SSL + CURLE_BAD_DOWNLOAD_RESUME, # 36 - couldn't resume download + CURLE_FILE_COULDNT_READ_FILE, # 37 + CURLE_LDAP_CANNOT_BIND, # 38 + CURLE_LDAP_SEARCH_FAILED, # 39 + CURLE_OBSOLETE40, # 40 - NOT USED + CURLE_FUNCTION_NOT_FOUND, # 41 - NOT USED starting with 7.53.0 + CURLE_ABORTED_BY_CALLBACK, # 42 + CURLE_BAD_FUNCTION_ARGUMENT, # 43 + CURLE_OBSOLETE44, # 44 - NOT USED + CURLE_INTERFACE_FAILED, # 45 - CURLOPT_INTERFACE failed + CURLE_OBSOLETE46, # 46 - NOT USED + CURLE_TOO_MANY_REDIRECTS, # 47 - catch endless re-direct loops + CURLE_UNKNOWN_OPTION, # 48 - User specified an unknown option + CURLE_SETOPT_OPTION_SYNTAX, # 49 - Malformed setopt option + CURLE_OBSOLETE50, # 50 - NOT USED + CURLE_OBSOLETE51, # 51 - NOT USED + CURLE_GOT_NOTHING, # 52 - when this is a specific error + CURLE_SSL_ENGINE_NOTFOUND, # 53 - SSL crypto engine not found + CURLE_SSL_ENGINE_SETFAILED, # 54 - can not set SSL crypto engine as + # default + CURLE_SEND_ERROR, # 55 - failed sending network data + CURLE_RECV_ERROR, # 56 - failure in receiving network data + CURLE_OBSOLETE57, # 57 - NOT IN USE + CURLE_SSL_CERTPROBLEM, # 58 - problem with the local certificate + CURLE_SSL_CIPHER, # 59 - couldn't use specified cipher + CURLE_PEER_FAILED_VERIFICATION, # 60 - peer's certificate or fingerprint + # wasn't verified fine + CURLE_BAD_CONTENT_ENCODING, # 61 - Unrecognized/bad encoding + CURLE_OBSOLETE62, # 62 - NOT IN USE since 7.82.0 + CURLE_FILESIZE_EXCEEDED, # 63 - Maximum file size exceeded + CURLE_USE_SSL_FAILED, # 64 - Requested FTP SSL level failed + CURLE_SEND_FAIL_REWIND, # 65 - Sending the data requires a rewind + # that failed + CURLE_SSL_ENGINE_INITFAILED, # 66 - failed to initialise ENGINE + CURLE_LOGIN_DENIED, # 67 - user, password or similar was not + # accepted and we failed to login + CURLE_TFTP_NOTFOUND, # 68 - file not found on server + CURLE_TFTP_PERM, # 69 - permission problem on server + CURLE_REMOTE_DISK_FULL, # 70 - out of disk space on server + CURLE_TFTP_ILLEGAL, # 71 - Illegal TFTP operation + CURLE_TFTP_UNKNOWNID, # 72 - Unknown transfer ID + CURLE_REMOTE_FILE_EXISTS, # 73 - File already exists + CURLE_TFTP_NOSUCHUSER, # 74 - No such user + CURLE_CONV_FAILED, # 75 - conversion failed + CURLE_OBSOLETE76, # 76 - NOT IN USE since 7.82.0 + CURLE_SSL_CACERT_BADFILE, # 77 - could not load CACERT file, missing + # or wrong format + CURLE_REMOTE_FILE_NOT_FOUND, # 78 - remote file not found + CURLE_SSH, # 79 - error from the SSH layer, somewhat + # generic so the error message will be of + # interest when this has happened + + CURLE_SSL_SHUTDOWN_FAILED, # 80 - Failed to shut down the SSL + # connection + CURLE_AGAIN, # 81 - socket is not ready for send/recv, + # wait till it's ready and try again (Added + # in 7.18.2) + CURLE_SSL_CRL_BADFILE, # 82 - could not load CRL file, missing or + # wrong format (Added in 7.19.0) + CURLE_SSL_ISSUER_ERROR, # 83 - Issuer check failed. (Added in + # 7.19.0) + CURLE_FTP_PRET_FAILED, # 84 - a PRET command failed + CURLE_RTSP_CSEQ_ERROR, # 85 - mismatch of RTSP CSeq numbers + CURLE_RTSP_SESSION_ERROR, # 86 - mismatch of RTSP Session Ids + CURLE_FTP_BAD_FILE_LIST, # 87 - unable to parse FTP file list + CURLE_CHUNK_FAILED, # 88 - chunk callback reported error + CURLE_NO_CONNECTION_AVAILABLE, # 89 - No connection available, the + # session will be queued + CURLE_SSL_PINNEDPUBKEYNOTMATCH, # 90 - specified pinned public key did not + # match + CURLE_SSL_INVALIDCERTSTATUS, # 91 - invalid certificate status + CURLE_HTTP2_STREAM, # 92 - stream error in HTTP/2 framing layer + CURLE_RECURSIVE_API_CALL, # 93 - an api function was called from + # inside a callback + CURLE_AUTH_ERROR, # 94 - an authentication function returned an + # error + CURLE_HTTP3, # 95 - An HTTP/3 layer problem + CURLE_QUIC_CONNECT_ERROR, # 96 - QUIC connection error + CURLE_PROXY, # 97 - proxy handshake error + CURLE_SSL_CLIENTCERT, # 98 - client-side certificate required + CURLE_UNRECOVERABLE_POLL, # 99 - poll/select returned fatal error + CURL_LAST # never use! + + curl_ftpmethod* {.size: sizeof(clong).} = enum + CURLFTPMETHOD_DEFAULT, # let libcurl pick + CURLFTPMETHOD_MULTICWD, # single CWD operation for each path part + CURLFTPMETHOD_NOCWD, # no CWD at all + CURLFTPMETHOD_SINGLECWD, # one CWD to full dir, then work on file + + CURLMcode* {.size: sizeof(cint).} = enum + CURLM_CALL_MULTI_PERFORM = -1, # please call curl_multi_perform() or + # curl_multi_socket*() soon + CURLM_OK, + CURLM_BAD_HANDLE, # the passed-in handle is not a valid CURLM handle + CURLM_BAD_EASY_HANDLE, # an easy handle was not good/valid + CURLM_OUT_OF_MEMORY, # if you ever get this, you're in deep sh*t + CURLM_INTERNAL_ERROR, # this is a libcurl bug + CURLM_BAD_SOCKET, # the passed in socket argument did not match + CURLM_UNKNOWN_OPTION, # curl_multi_setopt() with unsupported option + CURLM_ADDED_ALREADY, # an easy handle already added to a multi handle was + # attempted to get added - again + CURLM_RECURSIVE_API_CALL, # an api function was called from inside a + # callback + CURLM_WAKEUP_FAILURE, # wakeup is unavailable or failed + CURLM_BAD_FUNCTION_ARGUMENT, # function called with a bad parameter + CURLM_ABORTED_BY_CALLBACK, + CURLM_UNRECOVERABLE_POLL, + CURLM_LAST + + CURLMSG_E* {.size: sizeof(cint).} = enum + CURLMSG_NONE # first, not used + CURLMSG_DONE # This easy handle has completed. 'result' contains + # the CURLcode of the transfer + CURLMSG_LAST # last, not used + + CURLUcode* {.size: sizeof(cint).} = enum + CURLUE_OK, + CURLUE_BAD_HANDLE # 1 + CURLUE_BAD_PARTPOINTER # 2 + CURLUE_MALFORMED_INPUT # 3 + CURLUE_BAD_PORT_NUMBER # 4 + CURLUE_UNSUPPORTED_SCHEME # 5 + CURLUE_URLDECODE # 6 + CURLUE_OUT_OF_MEMORY # 7 + CURLUE_USER_NOT_ALLOWED # 8 + CURLUE_UNKNOWN_PART # 9 + CURLUE_NO_SCHEME # 10 + CURLUE_NO_USER # 11 + CURLUE_NO_PASSWORD # 12 + CURLUE_NO_OPTIONS # 13 + CURLUE_NO_HOST # 14 + CURLUE_NO_PORT # 15 + CURLUE_NO_QUERY # 16 + CURLUE_NO_FRAGMENT # 17 + CURLUE_NO_ZONEID # 18 + CURLUE_BAD_FILE_URL # 19 + CURLUE_BAD_FRAGMENT # 20 + CURLUE_BAD_HOSTNAME # 21 + CURLUE_BAD_IPV6 # 22 + CURLUE_BAD_LOGIN # 23 + CURLUE_BAD_PASSWORD # 24 + CURLUE_BAD_PATH # 25 + CURLUE_BAD_QUERY # 26 + CURLUE_BAD_SCHEME # 27 + CURLUE_BAD_SLASHES # 28 + CURLUE_BAD_USER # 29 + CURLUE_LACKS_IDN # 30 + CURLUE_LAST + + CURLUPart* {.size: sizeof(cint).} = enum + CURLUPART_URL + CURLUPART_SCHEME + CURLUPART_USER + CURLUPART_PASSWORD + CURLUPART_OPTIONS + CURLUPART_HOST + CURLUPART_PORT + CURLUPART_PATH + CURLUPART_QUERY + CURLUPART_FRAGMENT + CURLUPART_ZONEID # added in 7.65.0 + + curl_header* = object + name*: cstring + value*: cstring + amount*: csize_t + index*: csize_t + origin*: cint + anchor*: pointer + +proc `==`*(a: CURL; b: CURL): bool {.borrow.} +proc `==`*(a: CURL; b: typeof(nil)): bool {.borrow.} +proc `==`*(a: CURLM; b: CURLM): bool {.borrow.} +proc `==`*(a: CURLM; b: typeof(nil)): bool {.borrow.} + +{.push importc.} + +proc curl_global_init*(flags: clong): CURLcode +proc curl_global_cleanup*() +proc curl_free*(p: pointer) + +proc curl_easy_init*(): CURL +proc curl_easy_cleanup*(handle: CURL) +proc curl_easy_setopt*(handle: CURL; option: CURLoption): CURLcode {.varargs.} +proc curl_easy_perform*(handle: CURL): CURLcode +proc curl_easy_getinfo*(handle: CURL; info: CURLINFO): CURLcode {.varargs.} +proc curl_easy_strerror*(errornum: CURLcode): cstring +proc curl_easy_nextheader*(curl: CURL; origin: cuint; request: cint; + prev: ptr curl_header): ptr curl_header +proc curl_easy_header*(curl: CURL; name: cstring; index: csize_t; origin: cuint; + request: cint; hout: out ptr curl_header): cint + +proc curl_url*(): CURLU +proc curl_url_cleanup*(handle: CURLU) +proc curl_url_dup*(inh: CURLU): CURLU +proc curl_url_get*(handle: CURLU; what: CURLUPart; part: ptr cstring; + flags: cuint): CURLUcode +proc curl_url_set*(handle: CURLU; what: CURLUPart; part: cstring; + flags: cuint): CURLUcode +proc curl_url_strerror*(code: CURLUcode): cstring + +proc curl_mime_init*(handle: CURL): curl_mime +proc curl_mime_free*(mime: curl_mime) +proc curl_mime_addpart*(mime: curl_mime): curl_mimepart +proc curl_mime_name*(part: curl_mimepart; name: cstring) +proc curl_mime_data*(part: curl_mimepart; data: pointer; datasize: csize_t) +proc curl_mime_filename*(part: curl_mimepart; name: cstring) +proc curl_mime_filedata*(part: curl_mimepart; filename: cstring) + +proc curl_slist_append*(slist: curl_slist; str: cstring): curl_slist +proc curl_slist_free_all*(slist: curl_slist) + +proc curl_multi_init*(): CURLM +proc curl_multi_add_handle*(multi_handle: CURLM; curl_handle: CURL): CURLMcode +proc curl_multi_remove_handle*(multi_handle: CURLM; curl_handle: CURL): CURLMcode +proc curl_multi_fdset*(multi_handle: CURLM; read_fd_set, write_fd_set, + exc_fd_set: pointer; max_fd: ptr cint): CURLMcode +proc curl_multi_wait*(multi_handle: CURLM; extra_fds: ptr curl_waitfd; + extra_nfds: cuint; timeout_ns: cint; ret: ptr cint): CURLMcode +proc curl_multi_poll*(multi_handle: CURLM; extra_fds: ptr curl_waitfd; + extra_nfds: cuint; timeout_ns: cint; ret: ptr cint): CURLMcode +proc curl_multi_wakeup*(multi_handle: CURLM): CURLMcode +proc curl_multi_perform*(multi_handle: CURLM; running_handles: ptr cint): + CURLMcode +proc curl_multi_cleanup*(multi_handle: CURLM): CURLMcode +proc curl_multi_info_read*(multi_handle: CURLM; msgs_in_queue: ptr cint): CURLMsg +proc curl_multi_strerror*(code: CURLMcode): cstring +{.pop.} + +{.pop.} + + +template setopt(curl: CURL; opt: CURLoption; arg: typed) = + discard curl_easy_setopt(curl, opt, arg) + +template setopt(curl: CURL; opt: CURLoption; arg: string) = + discard curl_easy_setopt(curl, opt, cstring(arg)) + +template getinfo(curl: CURL; info: CURLINFO; arg: typed) = + discard curl_easy_getinfo(curl, info, arg) + +template set(url: CURLU; part: CURLUPart; content: cstring; flags: cuint) = + discard curl_url_set(url, part, content, flags) + +template set(url: CURLU; part: CURLUPart; content: string; flags: cuint) = + url.set(part, cstring(content), flags) + +func curlErrorToChaError(res: CURLcode): string = + return case res + of CURLE_OK: "" + of CURLE_URL_MALFORMAT: "InvalidURL" #TODO should never occur... + of CURLE_COULDNT_CONNECT: "ConnectionRefused" + of CURLE_COULDNT_RESOLVE_PROXY: "FailedToResolveProxy" + of CURLE_COULDNT_RESOLVE_HOST: "FailedToResolveHost" + of CURLE_PROXY: "ProxyRefusedToConnect" + else: "InternalError" + +proc getCurlConnectionError(res: CURLcode): string = + let e = curlErrorToChaError(res) + let msg = $curl_easy_strerror(res) + return "Cha-Control: ConnectionError " & e & " " & msg & "\n" + +type + EarlyHintState = enum + ehsNone, ehsStarted, ehsDone + + HttpHandle = ref object + curl: CURL + statusline: bool + connectreport: bool + earlyhint: EarlyHintState + slist: curl_slist + +const STDIN_FILENO = 0 +const STDOUT_FILENO = 1 + +proc writeAll(data: pointer; size: int) = + var n = 0 + while n < size: + let i = write(STDOUT_FILENO, addr cast[ptr UncheckedArray[uint8]](data)[n], + int(size) - n) + assert i >= 0 + n += i + +proc puts(s: string) = + if s.len > 0: + writeAll(unsafeAddr s[0], s.len) + +proc curlWriteHeader(p: cstring; size, nitems: csize_t; userdata: pointer): + csize_t {.cdecl.} = + var line = newString(nitems) + if nitems > 0: + copyMem(addr line[0], p, nitems) + let op = cast[HttpHandle](userdata) + if not op.statusline: + op.statusline = true + var status: clong + op.curl.getinfo(CURLINFO_RESPONSE_CODE, addr status) + if status == 103: + op.earlyhint = ehsStarted + else: + op.connectreport = true + puts("Status: " & $status & "\nCha-Control: ControlDone\n") + return nitems + if line == "\r\n" or line == "\n": + # empty line (last, before body) + if op.earlyhint == ehsStarted: + # ignore; we do not have a way to stream headers yet. + op.earlyhint = ehsDone + # reset statusline; we are awaiting the next line. + op.statusline = false + return nitems + puts("\r\n") + return nitems + + if op.earlyhint != ehsStarted: + # Regrettably, we can only write early hint headers after the status + # code is already known. + # For now, it seems easiest to just ignore them all. + puts(line) + return nitems + +# From the documentation: size is always 1. +proc curlWriteBody(p: cstring; size, nmemb: csize_t; userdata: pointer): + csize_t {.cdecl.} = + return csize_t(write(STDOUT_FILENO, p, int(nmemb))) + +# From the documentation: size is always 1. +proc readFromStdin(p: pointer; size, nitems: csize_t; userdata: pointer): + csize_t {.cdecl.} = + return csize_t(read(STDIN_FILENO, p, int(nitems))) + +proc curlPreRequest(clientp: pointer; conn_primary_ip, conn_local_ip: cstring; + conn_primary_port, conn_local_port: cint): cint {.cdecl.} = + let op = cast[HttpHandle](clientp) + op.connectreport = true + puts("Cha-Control: Connected\n") + return 0 # ok + +func startsWithIgnoreCase(s1, s2: openArray[char]): bool = + if s1.len < s2.len: return false + for i in 0 ..< s2.len: + if s1[i].toLowerAscii() != s2[i].toLowerAscii(): + return false + return true + +proc main() = + let curl = curl_easy_init() + doAssert curl != nil + let url = curl_url() + const flags = cuint(CURLU_PATH_AS_IS) + url.set(CURLUPART_SCHEME, getEnv("MAPPED_URI_SCHEME"), flags) + let username = getEnv("MAPPED_URI_USERNAME") + if username != "": + url.set(CURLUPART_USER, username, flags) + let password = getEnv("MAPPED_URI_PASSWORD") + if password != "": + url.set(CURLUPART_PASSWORD, password, flags) + url.set(CURLUPART_HOST, getEnv("MAPPED_URI_HOST"), flags) + let port = getEnv("MAPPED_URI_PORT") + if port != "": + url.set(CURLUPART_PORT, port, flags) + let path = getEnv("MAPPED_URI_PATH") + if path != "": + url.set(CURLUPART_PATH, path, flags) + let query = getEnv("MAPPED_URI_QUERY") + if query != "": + url.set(CURLUPART_QUERY, query, flags) + if getEnv("CHA_INSECURE_SSL_NO_VERIFY") == "1": + curl.setopt(CURLOPT_SSL_VERIFYPEER, 0) + curl.setopt(CURLOPT_SSL_VERIFYHOST, 0) + curl.setopt(CURLOPT_CURLU, url) + let op = HttpHandle(curl: curl) + curl.setopt(CURLOPT_SUPPRESS_CONNECT_HEADERS, 1) + curl.setopt(CURLOPT_WRITEFUNCTION, curlWriteBody) + curl.setopt(CURLOPT_HEADERDATA, op) + curl.setopt(CURLOPT_HEADERFUNCTION, curlWriteHeader) + curl.setopt(CURLOPT_PREREQDATA, op) + curl.setopt(CURLOPT_PREREQFUNCTION, curlPreRequest) + curl.setopt(CURLOPT_NOSIGNAL, 1) + let proxy = getEnv("ALL_PROXY") + if proxy != "": + curl.setopt(CURLOPT_PROXY, proxy) + case getEnv("REQUEST_METHOD") + of "GET": + curl.setopt(CURLOPT_HTTPGET, 1) + of "POST": + curl.setopt(CURLOPT_POST, 1) + let len = parseInt(getEnv("CONTENT_LENGTH")) + # > For any given platform/compiler curl_off_t must be typedef'ed to + # a 64-bit + # > wide signed integral data type. The width of this data type must remain + # > constant and independent of any possible large file support settings. + # > + # > As an exception to the above, curl_off_t shall be typedef'ed to + # a 32-bit + # > wide signed integral data type if there is no 64-bit type. + # It seems safe to assume that if the platform has no uint64 then Nim won't + # compile either. In return, we are allowed to post >2G of data. + curl.setopt(CURLOPT_POSTFIELDSIZE_LARGE, uint64(len)) + curl.setopt(CURLOPT_READFUNCTION, readFromStdin) + else: discard #TODO + let headers = getEnv("REQUEST_HEADERS") + for line in headers.split("\r\n"): + const needle = "Accept-Encoding: " + if line.startsWithIgnoreCase(needle): + let s = line.substr(needle.len) + # From the CURLOPT_ACCEPT_ENCODING manpage: + # > The application does not have to keep the string around after + # > setting this option. + curl.setopt(CURLOPT_ACCEPT_ENCODING, cstring(s)) + # This is OK, because curl_slist_append strdup's line. + op.slist = curl_slist_append(op.slist, cstring(line)) + if op.slist != nil: + curl.setopt(CURLOPT_HTTPHEADER, op.slist) + let res = curl_easy_perform(curl) + if res != CURLE_OK and not op.connectreport: + puts(getCurlConnectionError(res)) + op.connectreport = true + curl_easy_cleanup(curl) + +main() diff --git a/bonus/newhttp/http.nim b/bonus/newhttp/http.nim deleted file mode 100644 index e10c061b..00000000 --- a/bonus/newhttp/http.nim +++ /dev/null @@ -1,377 +0,0 @@ -import std/envvars -import std/options -import std/posix -import std/strutils - -import io/dynstream -import types/opt -import utils/sandbox - -import adapter/protocol/lcgi_ssl - -# tinfl bindings, see tinfl.h for details -const - TINFL_MAX_HUFF_TABLES = 3 - TINFL_MAX_HUFF_SYMBOLS_0 = 288 - TINFL_MAX_HUFF_SYMBOLS_1 = 32 - TINFL_FAST_LOOKUP_BITS = 10 - TINFL_FAST_LOOKUP_SIZE = 1 shl TINFL_FAST_LOOKUP_BITS - -const TINFL_LZ_DICT_SIZE = 32768 - -const - TINFL_FLAG_PARSE_ZLIB_HEADER = 0x01u32 - TINFL_FLAG_PARSE_GZIP_HEADER = 0x02u32 - TINFL_FLAG_HAS_MORE_INPUT = 0x04u32 - TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF* = 0x08u32 - -type tinfl_status {.size: sizeof(cint).} = enum - TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS = -5 - TINFL_STATUS_BAD_PARAM = -4 - TINFL_STATUS_ISIZE_OR_CRC32_MISMATCH = -3 - TINFL_STATUS_ADLER32_MISMATCH = -2 - TINFL_STATUS_FAILED = -1 - TINFL_STATUS_DONE = 0 - TINFL_STATUS_NEEDS_MORE_INPUT = 1 - TINFL_STATUS_HAS_MORE_OUTPUT = 2 - -type tinfl_huff_table {.importc, header: "tinfl.h", completeStruct.} = object - m_code_size: array[TINFL_MAX_HUFF_SYMBOLS_0, uint8] - m_look_up: array[TINFL_FAST_LOOKUP_SIZE, uint16] - m_tree: array[TINFL_MAX_HUFF_SYMBOLS_0 * 2, uint16] - -type tinfl_decompressor {.importc, header: "tinfl.h", completeStruct.} = object - m_state, m_num_bits, m_zhdr0, m_zhdr1, m_g_isize: uint32 - m_checksum, m_checksum_current: uint32 - m_final, m_type, m_check_adler32, m_dist, m_counter, m_num_extra: uint32 - m_table_sizes: array[TINFL_MAX_HUFF_TABLES, uint32] - - m_bit_buf: uint64 - m_dist_from_out_buf_start: csize_t - m_tables: array[TINFL_MAX_HUFF_TABLES, tinfl_huff_table] - - m_raw_header: array[4, uint8] - m_len_codes: array[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137, - uint8] - m_gz_header: array[10, uint8] - -{.push importc, cdecl, header: """ -#define TINFL_IMPLEMENTATION -#include "tinfl.h" -""".} -proc tinfl_decompress(r: var tinfl_decompressor; pIn_buf_next: ptr uint8; - pIn_buf_size: var csize_t; pOut_buf_start, pOut_buf_next: ptr uint8; - pOut_buf_size: var csize_t; decomp_flags: uint32): tinfl_status -{.pop.} # importc, cdecl, header: "tinfl.h" - -const InputBufferSize = 16384 - -type - HTTPHandle = ref object - state: HTTPState - bodyState: HTTPState # if TE is chunked, hsChunkSize; else hsBody - lineState: LineState - chunkSize: uint64 # Content-Length if TE is not chunked - ps: DynStream - os: PosixStream - line: string - headers: seq[tuple[key, value: string]] - - LineState = enum - lsNone, lsCrSeen - - HTTPState = enum - hsStatus, hsHeaders, hsChunkSize, hsAfterChunk, hsBody, hsTrailers, hsDone - - ContentEncoding = enum - ceGzip = "gzip" - ceDeflate = "deflate" - - TransferEncoding = enum - teChunked = "chunked" - teGzip = "gzip" - teDeflate = "deflate" - -proc inflate(op: HTTPHandle; flag: uint32) = - var pipefd {.noinit.}: array[2, cint] - if pipe(pipefd) != 0: - return - let pins = newPosixStream(pipefd[0]) - let pouts = newPosixStream(pipefd[1]) - case fork() - of -1: - pins.sclose() - pouts.sclose() - of 0: # child - enterNetworkSandbox() - pouts.sclose() - let os = op.os - var flags = flag or TINFL_FLAG_HAS_MORE_INPUT - var decomp = tinfl_decompressor() - var iq {.noinit.}: array[InputBufferSize, uint8] - var oq {.noinit.}: array[TINFL_LZ_DICT_SIZE, uint8] - var oqoff = 0 - while true: - let len0 = pins.readData(iq) - if len0 <= 0: - break - let len = csize_t(len0) - var n = csize_t(0) - while n < len: - var iqn = csize_t(len) - n - var oqn = csize_t(oq.len - oqoff) - let status = decomp.tinfl_decompress(addr iq[n], iqn, addr oq[0], - addr oq[oqoff], oqn, flags) - if not os.writeDataLoop(oq.toOpenArray(oqoff, oqoff + int(oqn) - 1)): - quit(1) - oqoff = int((csize_t(oqoff) + oqn) and csize_t(oq.len) - 1) - n += iqn - case status - of TINFL_STATUS_HAS_MORE_OUTPUT: - discard - of TINFL_STATUS_NEEDS_MORE_INPUT: - assert len == n - of TINFL_STATUS_DONE: - quit(0) - of TINFL_STATUS_BAD_PARAM: assert false - of TINFL_STATUS_ADLER32_MISMATCH, TINFL_STATUS_FAILED, - TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS, - TINFL_STATUS_ISIZE_OR_CRC32_MISMATCH: - stderr.writeLine("NewHTTP error: " & $status) - quit(1) - quit(0) - else: # parent - pins.sclose() - op.os = pouts - -proc handleStatus(op: HTTPHandle; iq: openArray[char]): int = - for i, c in iq: - case op.lineState - of lsNone: - if c == '\r': - op.lineState = lsCrSeen - else: - op.line &= c - of lsCrSeen: - if c != '\n' or - not op.line.startsWithIgnoreCase("HTTP/1.1") and - not op.line.startsWithIgnoreCase("HTTP/1.0"): - quit(1) - let codes = op.line.until(' ', "HTTP/1.0 ".len) - let code = parseUInt16(codes) - if codes.len > 3 or code.isNone: - quit(1) - let buf = "Status: " & $code.get & "\r\nCha-Control: ControlDone\r\n" - if not op.os.writeDataLoop(buf): - quit(1) - op.lineState = lsNone - op.state = hsHeaders - op.line = "" - return i + 1 - return iq.len - -proc handleHeaders(op: HTTPHandle; iq: openArray[char]): int = - for i, c in iq: - case op.lineState - of lsNone: - if c == '\r': - op.lineState = lsCrSeen - else: - op.line &= c - of lsCrSeen: - if c != '\n': # malformed header - quit(1) - if op.line != "": - var name = op.line.until(':') - if name.len > 0 and name[0] notin HTTPWhitespace and - name.len != op.line.len: - name = name.strip(leading = false, trailing = true, - chars = HTTPWhitespace) - let value = op.line.after(':').strip(leading = true, - trailing = false, chars = HTTPWhitespace) - op.headers.add((move(name), value)) - op.line = "" - op.lineState = lsNone - else: - var buf = "" - var contentEncodings: seq[ContentEncoding] = @[] - var transferEncodings: seq[TransferEncoding] = @[] - var contentLength = uint64.high - for it in op.headers: - buf &= it.key & ": " & it.value & "\r\n" - if it.key.equalsIgnoreCase("Content-Encoding"): - for it in it.value.split(','): - let x = parseEnumNoCase[ContentEncoding](it) - if x.isSome: - contentEncodings.add(x.get) - elif it.key.equalsIgnoreCase("Transfer-Encoding"): - for it in it.value.split(','): - let x = parseEnumNoCase[TransferEncoding](it) - if x.isSome: - transferEncodings.add(x.get) - elif it.key.equalsIgnoreCase("Content-Length"): - contentLength = parseUInt64(it.value).get(uint64.high) - buf &= "\r\n" - if not op.os.writeDataLoop(buf): - quit(1) - for i in countdown(contentEncodings.high, 0): - case contentEncodings[i] - of ceGzip: op.inflate(TINFL_FLAG_PARSE_GZIP_HEADER) - of ceDeflate: op.inflate(TINFL_FLAG_PARSE_ZLIB_HEADER) - op.bodyState = hsBody - for i in countdown(transferEncodings.high, 0): - case transferEncodings[i] - of teChunked: - if i == 0: - op.bodyState = hsChunkSize - of teGzip: op.inflate(TINFL_FLAG_PARSE_GZIP_HEADER) - of teDeflate: op.inflate(TINFL_FLAG_PARSE_ZLIB_HEADER) - op.lineState = lsNone - op.state = op.bodyState - if op.bodyState == hsBody: - op.chunkSize = contentLength - return i + 1 - return iq.len - -proc handleChunkSize(op: HTTPHandle; iq: openArray[char]): int = - for i, c in iq: - case op.lineState - of lsNone: - if c == '\r': - op.lineState = lsCrSeen - else: - let n = hexValue(c) - let osize = op.chunkSize - op.chunkSize = osize * 0x10 + uint64(n) - if n == -1 or osize > op.chunkSize: - stderr.writeLine("NewHTTP error: error decoding chunk size") - quit(1) - of lsCrSeen: - if c != '\n': - stderr.writeLine("NewHTTP error: CRLF expected") - quit(1) - op.lineState = lsNone - if op.chunkSize > 0: - op.state = hsBody - return i + 1 - op.state = hsTrailers - break - return iq.len - -proc handleBody(op: HTTPHandle; iq: openArray[char]): int = - var L = uint64(iq.len) - if L >= op.chunkSize: - L = op.chunkSize - op.state = hsAfterChunk - let n = int(L) - if not op.os.writeDataLoop(iq.toOpenArray(0, n - 1)): - quit(1) - op.chunkSize -= L - if op.bodyState == hsBody and op.chunkSize == 0: - return -1 # we're done - return n - -proc handleAfterChunk(op: HTTPHandle; iq: openArray[char]): int = - for i, c in iq: - case op.lineState - of lsNone: - if c != '\r': - quit(1) - op.lineState = lsCrSeen - of lsCrSeen: - if c != '\n': - quit(1) - op.lineState = lsNone - op.state = hsChunkSize - return i + 1 - return iq.len - -proc handleTrailers(op: HTTPHandle; iq: openArray[char]): int = - for i, c in iq: - case op.lineState - of lsNone: - if c == '\r': - op.lineState = lsCrSeen - else: - op.line &= c - of lsCrSeen: - if c != '\n': - quit(1) - op.lineState = lsNone - if op.line == "": - op.state = hsDone - return i + 1 - op.line = "" - return iq.len - -proc handleBuffer(op: HTTPHandle; iq: openArray[char]): int = - case op.state - of hsStatus: return op.handleStatus(iq) - of hsHeaders: return op.handleHeaders(iq) - of hsChunkSize: return op.handleChunkSize(iq) - of hsBody: return op.handleBody(iq) - of hsAfterChunk: return op.handleAfterChunk(iq) # CRLF after a chunk - of hsTrailers: return op.handleTrailers(iq) - of hsDone: return -1 - -proc checkCert(os: PosixStream; ssl: ptr SSL) = - let res = SSL_get_verify_result(ssl) - if res != X509_V_OK: - let s = X509_verify_cert_error_string(res) - os.die("InvalidResponse", $s) - -proc main() = - let secure = getEnv("MAPPED_URI_SCHEME") == "https" - let username = getEnv("MAPPED_URI_USERNAME") - let password = getEnv("MAPPED_URI_PASSWORD") - let host = getEnv("MAPPED_URI_HOST") - let port = getEnvEmpty("MAPPED_URI_PORT", if secure: "443" else: "80") - let path = getEnvEmpty("MAPPED_URI_PATH", "/") - let query = getEnv("MAPPED_URI_QUERY") - let os = newPosixStream(STDOUT_FILENO) - let ps = if secure: - let ssl = os.connectSSLSocket(host, port, useDefaultCA = true) - if getEnv("CHA_INSECURE_SSL_NO_VERIFY") != "1": - os.checkCert(ssl) - newSSLStream(ssl) - else: - os.connectSocket(host, port) - let op = HTTPHandle(ps: ps, os: os) - let requestMethod = getEnv("REQUEST_METHOD") - var buf = requestMethod & ' ' & path - if query != "": - buf &= '?' & query - buf &= " HTTP/1.1\r\n" - buf &= "Host: " & host - if secure and port != "443" or not secure and port != "80": - buf &= ':' & port - buf &= "\r\n" - buf &= "Connection: close\r\n" - if username != "": - buf &= "Authorization: Basic " & btoa(username & ':' & password) & "\r\n" - let contentLength = getEnv("CONTENT_LENGTH") - if (let x = parseUInt64(contentLength); x.isSome): - buf &= "Content-Length: " & $x.get & "\r\n" - buf &= getEnv("REQUEST_HEADERS") - buf &= "\r\n" - if not op.ps.writeDataLoop(buf): - os.die("ConnectionRefused", "error sending request header") - var iq {.noinit.}: array[InputBufferSize, char] - if requestMethod == "POST": - let ps = newPosixStream(STDIN_FILENO) - while (let n = ps.readData(iq); n > 0): - if not op.ps.writeDataLoop(iq.toOpenArray(0, n - 1)): - os.die("ConnectionRefused", "error sending request body") - if not os.writeDataLoop("Cha-Control: Connected\r\n"): - quit(1) - block readResponse: - while (let n = ps.readData(iq); n > 0): - var m = 0 - while m < n: - let k = op.handleBuffer(iq.toOpenArray(m, n - 1)) - if k == -1: # hsDone - break readResponse - m += k - op.ps.sclose() - -main() diff --git a/doc/protocols.md b/doc/protocols.md index 8eeec6d5..f1d88c35 100644 --- a/doc/protocols.md +++ b/doc/protocols.md @@ -31,25 +31,24 @@ also find an appropriate HTML converter in `adapter/format`. ## HTTP -HTTP/s support is based on libcurl; supported features largely depend on -your libcurl version. - -The libcurl HTTP adapter can take arbitrary headers and POST data, is able -to use passed userinfo data (`https://username:password@example.org`), and -returns all headers and response body it receives from libcurl without -exception. - -It is possible to build the HTTP adapter using -[curl-impersonate](https://github.com/lwthiker/curl-impersonate) by setting -the compile-time variable CURLLIBNAME to `libcurl-impersonate.so`. Note that -for curl-impersonate to work, you must set `network.default-headers = {}` -in the Chawan config. (Otherwise, the libcurl adapter will happily override -curl-impersonate headers, which is probably not what you want.) - -The `bonus` directory contains an alternative HTTP client based on FreeBSD -libfetch. It is mostly a proof of concept, as FreeBSD libfetch HTTP support is -very limited; in particular, it does not support arbitrary HTTP headers, so e.g. -cookies will not work. +HTTP/s support is implemented in Nim. It supports HTTP/1.1 with +arbitrary headers and POST data, is able to use passed userinfo data, +and returns all headers and response body it receives without exception. + +Deflate decompression with gzip and zlib headers is supported. +(Content-Type: gzip, deflate.) This is based on a modified version of +the public domain tinfl.h decompressor by Rich Gelreich. + +The `bonus` directory contains two alternative HTTP clients: + +* curlhttp; this is the old HTTP client based on libcurl. It can be + built using curl-impersonate; see [README.md](../doc/README.md) in + the bonus/ directory for details. + +* libfetch-http: based on FreeBSD libfetch. It is mostly a proof of + concept, as FreeBSD libfetch HTTP support is very limited; in + particular, it does not support arbitrary HTTP headers, so e.g. + cookies will not work. ## FTP diff --git a/res/license.md b/res/license.md index 920f34e0..b8b648c3 100644 --- a/res/license.md +++ b/res/license.md @@ -11,9 +11,9 @@ terms. This document attempts to list such projects included in this repository. Usually, the Chawan executables are also dynamically linked against your -copy of libcurl, libssh2, OpenSSL or LibreSSL, your C library, and if -exists, your termcap library (normally ncurses). For licensing terms of -these, please consult the appropriate library's documentation. +copy of libssh2, OpenSSL or LibreSSL, your C library, and if exists, +your termcap library (normally ncurses). For licensing terms of these, +please consult the appropriate library's documentation. Also, Chawan is statically linked to parts of the Nim standard library, available (at the time of writing) under terms of the MIT license. @@ -22,6 +22,7 @@ Table of contents: * [Chawan](#chawan) * [STB image](#stb-image) +* [tinfl](#tinfl) * [JebP](#jebp) * [NanoSVG](#nanosvg) * [QuickJS-NG](#quickjs-ng) @@ -60,10 +61,20 @@ For more information, please refer to <http://unlicense.org/> ## STB image The stb_image, stb_image_write and stb_image_resize libraries by -Sean Barrett et al. are used for image transcoding. These libraries +Sean Barrett et al. are used for image transcoding. These libraries are dedicated to the public domain, and are distributed under the same terms as Chawan. +## tinfl + +tinfl.h is an inflate implementation in a single function by Rich +Geldreich, used in the HTTP/s client. This library is dedicated to the +public domain, and is distributed under the same terms as Chawan. + +(Notably, this is based on the old public domain miniz v1.16 beta +release, not the more recent MIT version. In practice, there are no +substantial differences between the two.) + ## JebP A slightly modified version of the JebP library by Jasmine Minter is used for |