R_API void r_socket_http_response (RSocketHTTPRequest *rs, int code, const char *out, int len, const char *headers) { const char *strcode = \ code==200?"ok": code==301?"moved permanently": code==302?"Found": code==404?"not found": "UNKNOWN"; if (len<1) len = out? strlen (out): 0; if (!headers) headers = ""; r_socket_printf (rs->s, "HTTP/1.0 %d %s\r\n%s" "Connection: close\r\nContent-Length: %d\r\n\r\n", code, strcode, headers, len); if (out && len>0) r_socket_write (rs->s, (void*)out, len); }
R_API char *r_socket_http_get (const char *url, int *code, int *rlen) { RSocket *s; int ssl = !memcmp (url, "https://", 8); char *response, *host, *path, *port = "80"; char *uri = strdup (url); if (!uri) return NULL; if (code) *code = 0; if (rlen) *rlen = 0; host = strstr (uri, "://"); if (!host) { free (uri); eprintf ("r_socket_http_get: Invalid URI"); return NULL; } host += 3; port = strchr (host, ':'); if (!port) { port = (ssl)?"443":"80"; path = host; } else { *port++ = 0; path = port; } path = strchr (path, '/'); if (!path) path = ""; else *path++ = 0; s = r_socket_new (ssl); if (!s) { eprintf ("r_socket_http_get: Cannot create socket\n"); free (uri); return NULL; } if (r_socket_connect_tcp (s, host, port, 0)) { r_socket_printf (s, "GET /%s HTTP/1.1\r\n" "User-Agent: radare2 "R2_VERSION"\r\n" "Accept: */*\r\n" "Host: %s:%s\r\n" "\r\n", path, host, port); response = r_socket_http_answer (s, code, rlen); } else { eprintf ("Cannot connect to %s:%s\n", host, port); response = NULL; } free (uri); r_socket_free (s); return response; }
R_API char *r_socket_http_post (const char *url, const char *data, int *code, int *rlen) { RSocket *s; int ssl = !memcmp (url, "https://", 8); char *response, *host, *path, *port = "80"; char *uri = strdup (url); if (!uri) return NULL; host = strstr (uri, "://"); if (!host) { free (uri); printf ("Invalid URI"); return NULL; } host += 3; port = strchr (host, ':'); if (!port) port = (ssl)?"443":"80"; else *port++ = 0; path = strchr (host, '/'); if (!path) path = ""; else *path++ = 0; s = r_socket_new (ssl); if (!s) { printf ("Cannot create socket\n"); free (uri); return NULL; } if (!r_socket_connect_tcp (s, host, port, 0)) { eprintf ("Cannot connect to %s:%s\n", host, port); free (uri); return NULL; } /* Send */ r_socket_printf (s, "POST /%s HTTP/1.0\r\n" "User-Agent: radare2 "R2_VERSION"\r\n" "Accept: */*\r\n" "Host: %s\r\n" "Content-Length: %i\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "\r\n", path, host, strlen (data)); r_socket_write (s, (void *)data, strlen (data)); response = r_socket_http_answer (s, code, rlen); free (uri); return response; }
static char *runcmd (const char *cmd) { char buf[4096] = {0}; if (cmd) { r_socket_printf (gs, "%s\n", cmd); } int timeout = 1000000; char * str = NULL; r_socket_block_time (gs, 1, timeout); while (true) { memset (buf, 0, sizeof (buf)); r_socket_read (gs, (ut8*)buf, sizeof (buf) - 1); // NULL-terminate the string always char *promptFound = strstr (buf, "Wine-dbg>"); if (promptFound) { *promptFound = 0; return r_str_append (str, buf); } str = r_str_append (str, buf); } return NULL; }