summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2017-06-16 11:30:36 +0200
committerDaniel Stenberg <daniel@haxx.se>2017-06-16 11:30:36 +0200
commit2789e1ff2af5ff368ee4ff3ba3607eca2f2c10c4 (patch)
tree121ffc90cc934c9b791842b867bb3e0bf55e31de
parentde4c747544ebcaa0d5358418eb4a47879a15ccd4 (diff)
downloadcurl-bagder/avoid-binary-to-terminal.tar.gz
curl: prevent binary output spewed to terminalbagder/avoid-binary-to-terminal
... unless "--output -" is used. Binary detection is done by simply checking for a binary zero in early data. Added test 1425 1426 to verify. Closes #1512
-rw-r--r--docs/TODO6
-rw-r--r--src/tool_cb_wrt.c23
-rw-r--r--src/tool_cfgable.h9
-rw-r--r--src/tool_getparam.c2
-rw-r--r--src/tool_help.c2
-rw-r--r--src/tool_main.c2
-rw-r--r--src/tool_operate.c8
-rw-r--r--tests/data/Makefile.inc2
-rw-r--r--tests/data/test1425bin0 -> 1726 bytes
-rw-r--r--tests/data/test1426bin0 -> 1663 bytes
10 files changed, 42 insertions, 12 deletions
diff --git a/docs/TODO b/docs/TODO
index 3e3ea1684..099929b36 100644
--- a/docs/TODO
+++ b/docs/TODO
@@ -140,7 +140,6 @@
18.4 simultaneous parallel transfers
18.5 provide formpost headers
18.6 warning when setting an option
- 18.7 warning when sending binary output to terminal
18.8 offer color-coded HTTP header output
18.9 Choose the name of file in braces for complex URLs
18.10 improve how curl works in a windows console window
@@ -933,11 +932,6 @@ that doesn't exist on the server, just like --ftp-create-dirs.
This can be useful to tell when support for a particular feature hasn't been
compiled into the library.
-18.7 warning when sending binary output to terminal
-
- Provide a way that prompts the user for confirmation before binary data is
- sent to the terminal, much in the style 'less' does it.
-
18.8 offer color-coded HTTP header output
By offering different color output on the header name and the header
diff --git a/src/tool_cb_wrt.c b/src/tool_cb_wrt.c
index 6c08943ea..cdd4bf8de 100644
--- a/src/tool_cb_wrt.c
+++ b/src/tool_cb_wrt.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -85,11 +85,20 @@ size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
* point returning a value different from sz*nmemb indicates failure.
*/
const size_t failure = (sz && nmemb) ? 0 : 1;
+ bool isatty = config->global->isatty;
if(!config)
return failure;
#ifdef DEBUGBUILD
+ {
+ char *tty = curlx_getenv("CURL_ISATTY");
+ if(tty) {
+ isatty = TRUE;
+ curl_free(tty);
+ }
+ }
+
if(config->include_headers) {
if(sz * nmemb > (size_t)CURL_MAX_HTTP_HEADER) {
warnf(config->global, "Header data size exceeds single call write "
@@ -137,6 +146,18 @@ size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
if(!outs->stream && !tool_create_output_file(outs))
return failure;
+ if(isatty && (outs->bytes < 2000) && !config->binary_ok) {
+ /* binary output to terminal? */
+ size_t bytes = sz * nmemb;
+ if(memchr(buffer, 0, bytes)) {
+ warnf(config->global, "Binary output can mess up your terminal. "
+ "Use \"--output -\" to tell curl to output it to your terminal "
+ "anyway, or consider \"--output <FILE>\" to save to a file.\n");
+ config->synthetic_error = ERR_BINARY_TERMINAL;
+ return bytes-1;
+ }
+ }
+
rc = fwrite(buffer, sz, nmemb, outs->stream);
if((sz * nmemb) == rc)
diff --git a/src/tool_cfgable.h b/src/tool_cfgable.h
index 38777f6fd..03b470ba7 100644
--- a/src/tool_cfgable.h
+++ b/src/tool_cfgable.h
@@ -27,6 +27,12 @@
#include "tool_metalink.h"
+typedef enum {
+ ERR_NONE,
+ ERR_BINARY_TERMINAL = 1, /* binary to terminal detected */
+ ERR_LAST
+} curl_error;
+
struct GlobalConfig;
struct OperationConfig {
@@ -141,6 +147,7 @@ struct OperationConfig {
bool insecure_ok; /* set TRUE to allow insecure SSL connects */
bool proxy_insecure_ok; /* set TRUE to allow insecure SSL connects
for proxy */
+ bool binary_ok;
bool verifystatus;
bool create_dirs;
bool ftp_create_dirs;
@@ -236,6 +243,8 @@ struct OperationConfig {
double expect100timeout;
bool suppress_connect_headers; /* suppress proxy CONNECT response headers
from user callbacks */
+ curl_error synthetic_error; /* if non-zero, it overrides any libcurl
+ error */
struct GlobalConfig *global;
struct OperationConfig *prev;
struct OperationConfig *next; /* Always last in the struct */
diff --git a/src/tool_getparam.c b/src/tool_getparam.c
index fabe8f04b..bcb9e1ee2 100644
--- a/src/tool_getparam.c
+++ b/src/tool_getparam.c
@@ -1554,7 +1554,7 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
config->proxy_insecure_ok = toggle;
break;
- case '9':
+ case '9': /* --proxy-tlsv1 */
/* TLS version 1 for proxy */
config->proxy_ssl_version = CURL_SSLVERSION_TLSv1;
break;
diff --git a/src/tool_help.c b/src/tool_help.c
index 46aae4527..08a81f590 100644
--- a/src/tool_help.c
+++ b/src/tool_help.c
@@ -252,7 +252,7 @@ static const struct helptxt helptext[] = {
"Use HTTP NTLM authentication"},
{" --ntlm-wb",
"Use HTTP NTLM authentication with winbind"},
- {" --oauth2-bearer",
+ {" --oauth2-bearer <token>",
"OAuth 2 Bearer Token"},
{"-o, --output <file>",
"Write to file instead of stdout"},
diff --git a/src/tool_main.c b/src/tool_main.c
index 7e742ffca..089a317d4 100644
--- a/src/tool_main.c
+++ b/src/tool_main.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
diff --git a/src/tool_operate.c b/src/tool_operate.c
index 6f1525e8b..c57984888 100644
--- a/src/tool_operate.c
+++ b/src/tool_operate.c
@@ -862,6 +862,9 @@ static CURLcode operate_do(struct GlobalConfig *global,
set_binmode(stdout);
}
+ /* explicitly passed to stdout means okaying binary gunk */
+ config->binary_ok = (outfile && !strcmp(outfile, "-"));
+
if(!config->tcp_nodelay)
my_setopt(curl, CURLOPT_TCP_NODELAY, 0L);
@@ -1764,7 +1767,10 @@ static CURLcode operate_do(struct GlobalConfig *global,
}
else
#endif
- if(result && global->showerror) {
+ if(config->synthetic_error) {
+ ;
+ }
+ else if(result && global->showerror) {
fprintf(global->errors, "curl: (%d) %s\n", result, (errorbuffer[0]) ?
errorbuffer : curl_easy_strerror(result));
if(result == CURLE_SSL_CACERT)
diff --git a/tests/data/Makefile.inc b/tests/data/Makefile.inc
index d5f892e4c..4f3428987 100644
--- a/tests/data/Makefile.inc
+++ b/tests/data/Makefile.inc
@@ -151,7 +151,7 @@ test1396 test1397 test1398 \
test1400 test1401 test1402 test1403 test1404 test1405 test1406 test1407 \
test1408 test1409 test1410 test1411 test1412 test1413 test1414 test1415 \
test1416 test1417 test1418 test1419 test1420 test1421 test1422 test1423 \
-test1424 \
+test1424 test1425 test1426 \
test1428 test1429 test1430 test1431 test1432 test1433 test1434 test1435 \
test1436 test1437 test1438 test1439 test1440 test1441 test1442 test1443 \
test1444 test1445 test1446 \
diff --git a/tests/data/test1425 b/tests/data/test1425
new file mode 100644
index 000000000..0044c69d5
--- /dev/null
+++ b/tests/data/test1425
Binary files differ
diff --git a/tests/data/test1426 b/tests/data/test1426
new file mode 100644
index 000000000..dd6a8d117
--- /dev/null
+++ b/tests/data/test1426
Binary files differ