Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable cURL's automatic decompression #166

Merged
merged 1 commit into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/pextlib1.0/curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
#define _CURL_MINIMUM_XFER_TIMEOUT ((long)(60)) /* 1 minute */
#define _CURL_MINIMUM_PROGRESS_INTERVAL ((double)(0.2)) /* 0.2 seconds */

#if defined CURLOPT_ACCEPT_ENCODING
#define _CURL_ENCODING CURLOPT_ACCEPT_ENCODING
#elif defined CURLOPT_ENCODING
#define _CURL_ENCODING CURLOPT_ENCODING
#endif

/* ========================================================================= **
* Definitions
* ========================================================================= */
Expand Down Expand Up @@ -150,7 +156,7 @@ SetResultFromCurlMErrorCode(Tcl_Interp *interp, CURLMcode inErrorCode)
/**
* curl fetch subcommand entry point.
*
* syntax: curl fetch [--disable-epsv] [--ignore-ssl-cert] [--remote-time] [-u userpass] [--effective-url lasturlvar] [--progress "builtin"|callback] url filename
* syntax: curl fetch [--disable-epsv] [--ignore-ssl-cert] [--remote-time] [-u userpass] [--effective-url lasturlvar] [--progress "builtin"|callback] [--disable-compression] url filename
*
* @param interp current interpreter
* @param objc number of parameters
Expand Down Expand Up @@ -191,6 +197,7 @@ CurlFetchCmd(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[])
struct curl_slist *headers = NULL;
struct CURLMsg *info = NULL;
int running; /* number of running transfers */
char* acceptEncoding = "";

/* we might have options and then the url and the file */
/* let's process the options first */
Expand Down Expand Up @@ -276,6 +283,8 @@ CurlFetchCmd(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[])
theResult = TCL_ERROR;
break;
}
} else if (strcmp(theOption, "--disable-compression") == 0) {
acceptEncoding = NULL;
} else {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "curl fetch: unknown option ", theOption, NULL);
Expand Down Expand Up @@ -487,6 +496,20 @@ CurlFetchCmd(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[])
}
}

/* we want compression by default, but can optionally disable it.
* a CURLOPT_ACCEPT_ENCODING of "" means to let cURL write the
* Accept-Encoding header for you, based on what the library
* was compiled to support.
* A value of NULL disables all attemps at decompressing responses.
*/
#ifdef _CURL_ENCODING
theCurlCode = curl_easy_setopt(theHandle, _CURL_ENCODING, acceptEncoding);
if (theCurlCode != CURLE_OK) {
theResult = SetResultFromCurlErrorCode(interp, theCurlCode);
break;
}
#endif

/* Clear the Pragma: no-cache header */
headers = curl_slist_append(headers, "Pragma:");
/* Append any optional headers */
Expand Down
26 changes: 22 additions & 4 deletions src/pextlib1.0/tests/curl.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,32 @@ proc main {pextlibname} {
#curl fetch -u "I accept www.opensource.org/licenses/cpl:." http://www.research.att.com/~gsf/download/tgz/sfio.2005-02-01.tgz $tempfile
#test {[md5 file $tempfile] == "48f45c7c77c23ab0ccca48c22b3870de"}

curl fetch https://www.whatsmyip.org/http-compression-test/ $tempfile
grepper $tempfile {gz_yes}

curl fetch --disable-compression https://www.whatsmyip.org/http-compression-test/ $tempfile
grepper $tempfile {gz_no}

file delete -force $tempfile
}

proc test {args} {
if {[catch {uplevel 1 expr $args} result] || !$result} {
puts "[uplevel 1 subst -nocommands $args] == $result"
exit 1
}
if {[catch {uplevel 1 expr $args} result] || !$result} {
puts "[uplevel 1 subst -nocommands $args] == $result"
exit 1
}
}

proc grepper {filepath regex} {
set hasMatch 0
set fd [open $filepath]
while {[gets $fd line] != -1} {
if {[regexp $regex $line all]} {
set hasMatch 1
}
}
close $fd
test {$hasMatch == 1}
}

main $argv