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 484be94d38068..8938606658a8f 100644 --- a/libr/socket/socket_http.c +++ b/libr/socket/socket_http.c @@ -95,33 +95,16 @@ static char *socket_http_answer(RSocket *s, int *code, int *rlen, ut32 redirecti } else { len = olen - (dn - buf); } - if (len == 0 || 1) { - char *res = calloc (olen, 1); - int total = 0; - do { - r_socket_block_time (s, true, 0, 0); - ret = r_socket_read (s, (ut8*) res + total, olen - total); - if (ret == -1) { - R_LOG_ERROR ("-1"); - break; - } - if (ret == 0) { - continue; - } - total += ret; - } while (total < olen); - eprintf ("%d\n", total); - return res; + if (len == 0) { + eprintf ("LEN = 0\n"); } - eprintf ("LEN I%d\n", len); if (len > 0) { - eprintf ("READING %d\n", len); 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); @@ -322,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) { @@ -353,7 +336,7 @@ R_API char *r_socket_http_post(const char *url, const char *data, int *code, int "Host: %s:%d\r\n" "Content-Length: %i\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" - "\r\n", path, host, atoi(port), (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);