diff --git a/binr/r2agent/r2agent.c b/binr/r2agent/r2agent.c index bb73ca76a5..93503d9250 100644 --- a/binr/r2agent/r2agent.c +++ b/binr/r2agent/r2agent.c @@ -101,7 +101,7 @@ int main(int argc, char **argv) { char *result_heap = NULL; const char *result = page_index; - rs = r_socket_http_accept (s, timeout); + rs = r_socket_http_accept (s, 0, timeout); if (!rs) continue; if (!strcmp (rs->method, "GET")) { if (!strncmp (rs->path, "/proc/kill/", 11)) { diff --git a/libr/core/rtr.c b/libr/core/rtr.c index 1111f1044f..f12af91f8b 100644 --- a/libr/core/rtr.c +++ b/libr/core/rtr.c @@ -467,8 +467,6 @@ static int r_core_rtr_http_run(RCore *core, int launch, const char *path) { return 1; } - r_socket_block_time (s, 1, 1); - if (launch=='H') { const char *browser = r_config_get (core->config, "http.browser"); r_sys_cmdf ("%s http://%s:%d/%s &", @@ -528,7 +526,8 @@ static int r_core_rtr_http_run(RCore *core, int launch, const char *path) { /* this is blocking */ activateDieTime (core); - rs = r_socket_http_accept (s, timeout); + + rs = r_socket_http_accept (s, 1, timeout); origoff = core->offset; origblk = core->block; diff --git a/libr/include/r_socket.h b/libr/include/r_socket.h index d7bfb07c0e..bfad4c85dd 100644 --- a/libr/include/r_socket.h +++ b/libr/include/r_socket.h @@ -92,6 +92,7 @@ R_API int r_socket_close(RSocket *s); R_API int r_socket_free(RSocket *s); R_API bool r_socket_listen(RSocket *s, const char *port, const char *certfile); R_API RSocket *r_socket_accept(RSocket *s); +R_API RSocket *r_socket_accept_timeout(RSocket *s, unsigned int timeout); R_API int r_socket_block_time(RSocket *s, int block, int sec); R_API int r_socket_flush(RSocket *s); R_API int r_socket_ready(RSocket *s, int secs, int usecs); @@ -136,7 +137,7 @@ typedef struct r_socket_http_request { int data_length; } RSocketHTTPRequest; -R_API RSocketHTTPRequest *r_socket_http_accept(RSocket *s, int timeout); +R_API RSocketHTTPRequest *r_socket_http_accept(RSocket *s, int accept_timeout, int timeout); R_API void r_socket_http_response(RSocketHTTPRequest *rs, int code, const char *out, int x, const char *headers); R_API void r_socket_http_close(RSocketHTTPRequest *rs); R_API ut8 *r_socket_http_handle_upload(const ut8 *str, int len, int *olen); diff --git a/libr/socket/http_server.c b/libr/socket/http_server.c index c0d9c8ca58..8538750e32 100644 --- a/libr/socket/http_server.c +++ b/libr/socket/http_server.c @@ -8,13 +8,17 @@ R_API void r_socket_http_server_set_breaked(bool *b) { breaked = b; } -R_API RSocketHTTPRequest *r_socket_http_accept (RSocket *s, int timeout) { +R_API RSocketHTTPRequest *r_socket_http_accept (RSocket *s, int accept_timeout, int timeout) { int content_length = 0, xx, yy; int pxx = 1, first = 0; char buf[1500], *p, *q; RSocketHTTPRequest *hr = R_NEW0 (RSocketHTTPRequest); if (!hr) return NULL; - hr->s = r_socket_accept (s); + if (accept_timeout > 0) { + hr->s = r_socket_accept_timeout (s, 1); + } else { + hr->s = r_socket_accept (s); + } if (!hr->s) { free (hr); return NULL; diff --git a/libr/socket/socket.c b/libr/socket/socket.c index 37852e64fd..87325c28e3 100644 --- a/libr/socket/socket.c +++ b/libr/socket/socket.c @@ -590,6 +590,30 @@ R_API RSocket *r_socket_accept(RSocket *s) { return sock; } +R_API RSocket *r_socket_accept_timeout(RSocket *s, unsigned int timeout) { + fd_set read_fds; + fd_set except_fds; + + FD_ZERO (&read_fds); + FD_SET (s->fd, &read_fds); + + FD_ZERO (&except_fds); + FD_SET (s->fd, &except_fds); + + struct timeval t; + t.tv_sec = timeout; + t.tv_usec = 0; + + int r = select (s->fd + 1, &read_fds, NULL, &except_fds, &t); + if(r < 0) { + perror ("select"); + } else if (r > 0 && FD_ISSET (s->fd, &read_fds)) { + return r_socket_accept (s); + } + + return NULL; +} + R_API int r_socket_block_time (RSocket *s, int block, int sec) { #if __UNIX__ || defined(__CYGWIN__) int ret, flags;