Skip to content

Commit

Permalink
Add curl post support and minor http fixes ##socket
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae committed Jun 20, 2024
1 parent 8f4630d commit 9a1ce9d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
23 changes: 21 additions & 2 deletions libr/core/cmd_cmp.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down Expand Up @@ -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");
Expand Down
23 changes: 13 additions & 10 deletions libr/socket/socket_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit 9a1ce9d

Please sign in to comment.