From 9a1ce9d60eced0a779d01a35c0cb7265653cae51 Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 20 Jun 2024 10:45:38 +0200 Subject: [PATCH] Add curl post support and minor http fixes ##socket --- libr/core/cmd_cmp.inc.c | 23 +++++++++++++++++++++-- libr/socket/socket_http.c | 23 +++++++++++++---------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/libr/core/cmd_cmp.inc.c b/libr/core/cmd_cmp.inc.c index b5b30c5a6d979..418df4c98dd16 100644 --- a/libr/core/cmd_cmp.inc.c +++ b/libr/core/cmd_cmp.inc.c @@ -60,7 +60,7 @@ static RCoreHelpMessage help_msg_cu = { "cu8", " $$+1 > p", "compare qwords from current seek and +1", "cud", " $$+1 > p", "compare disasm current seek and +1", "wu", " p", "apply unified hex patch (see output of cu)", - "curl", " [http-url]", "", + "curl", " ([-D data]) [http-url]", "", NULL }; @@ -1079,12 +1079,31 @@ static void cmd_curl(RCore *core, const char *arg) { if (r_sys_getenv_asbool ("R2_CURL")) { r_sys_cmdf ("curl %s", arg); } else { + char *postdata = NULL; + arg = r_str_trim_head_ro (arg); + if (r_str_startswith (arg, "-D")) { + if (arg[2] == ' ') { + arg = r_str_trim_head_ro (arg + 2); + const char *space = strchr (arg, ' '); + if (space) { + postdata = r_str_ndup (arg, space - arg); + arg = space + 1; + } + } + if (!postdata) { + r_core_cmd_help_match (core, help_msg_cu, "curl"); + return; + } + } if (r_str_startswith (arg, "http://") || r_str_startswith (arg, "https://")) { int len; - char *s = r_socket_http_get (arg, NULL, &len); + char *s = postdata + ? r_socket_http_post (arg, postdata, NULL, &len) + : r_socket_http_get (arg, NULL, &len); if (s) { r_cons_write (s, len); free (s); + r_cons_newline (); } } else { r_core_cmd_help_match (core, help_msg_cu, "curl"); diff --git a/libr/socket/socket_http.c b/libr/socket/socket_http.c index 8cc001a7aee56..8938606658a8f 100644 --- a/libr/socket/socket_http.c +++ b/libr/socket/socket_http.c @@ -95,13 +95,16 @@ static char *socket_http_answer(RSocket *s, int *code, int *rlen, ut32 redirecti } else { len = olen - (dn - buf); } + if (len == 0) { + eprintf ("LEN = 0\n"); + } if (len > 0) { if (len > olen) { res = malloc (len + 2); if (!res) { goto exit; } - olen -= dn - buf; + olen -= (dn - buf); memcpy (res, dn + delta, olen); do { ret = r_socket_read_block (s, (ut8*) res + olen, len - olen); @@ -302,16 +305,16 @@ R_API char *r_socket_http_post(const char *url, const char *data, int *code, int } host += 3; char *port = strchr (host, ':'); - if (!port) { - port = (ssl)? "443": "80"; - } else { - *port++ = 0; - } char *path = strchr (host, '/'); - if (!path) { - path = ""; + if (port && (!path || (path && port < path))) { + *port++ = 0; } else { + port = ssl? "443": "80"; + } + if (path) { *path++ = 0; + } else { + path = ""; } s = r_socket_new (ssl); if (!s) { @@ -330,10 +333,10 @@ R_API char *r_socket_http_post(const char *url, const char *data, int *code, int "POST /%s HTTP/1.0\r\n" "User-Agent: radare2 "R2_VERSION"\r\n" "Accept: */*\r\n" - "Host: %s\r\n" + "Host: %s:%d\r\n" "Content-Length: %i\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" - "\r\n", path, host, (int)strlen (data)); + "\r\n", path, host, atoi (port), (int)strlen (data)); free (uri); r_socket_write (s, (void *)data, strlen (data)); return socket_http_answer (s, code, rlen, 0);