Fix more issues in RSocket.printf affecting the webserver ##socket

This commit is contained in:
pancake 2021-02-06 21:59:18 +01:00
parent fba880de13
commit 38689963ad
3 changed files with 26 additions and 18 deletions

View File

@ -157,6 +157,7 @@ R_API RSocketHTTPRequest *r_socket_http_accept(RSocket *s, RSocketHTTPOptions *s
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);
R_API void r_socket_http_free(RSocketHTTPRequest *rs);
typedef int (*rap_server_open)(void *user, const char *file, int flg, int mode);
typedef int (*rap_server_seek)(void *user, ut64 offset, int whence);

View File

@ -762,23 +762,25 @@ R_API void r_socket_printf(RSocket *s, const char *fmt, ...) {
if (s->fd != R_INVALID_SOCKET) {
va_start (ap, fmt);
va_copy (ap0, ap);
size_t len = vsnprintf (NULL, 0, fmt, ap0) + 1;
char *buf = calloc (len, 1);
vsnprintf (buf, len, fmt, ap);
size_t left = len ;
size_t done = 0;
while (left >= 0) {
int res = r_socket_write (s, buf + done, left);
if (res < 1) {
break;
size_t len = vsnprintf (NULL, 0, fmt, ap0);
char *buf = calloc (len + 1, 1);
if (buf) {
vsnprintf (buf, len + 1, fmt, ap);
size_t left = len;
size_t done = 0;
while (left > 0) {
int res = r_socket_write (s, buf + done, left);
if (res < 1) {
break;
}
if (res == left) {
break;
}
left -= res;
done += res;
}
if (res == left) {
break;
}
left -= res;
done += res;
free (buf);
}
free (buf);
va_end (ap);
}
}

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2012-2016 - pancake */
/* radare - LGPL - Copyright 2012-2021 - pancake */
#include <r_socket.h>
#include <r_util.h>
@ -118,7 +118,7 @@ R_API RSocketHTTPRequest *r_socket_http_accept (RSocket *s, RSocketHTTPOptions *
return hr;
}
R_API void r_socket_http_response (RSocketHTTPRequest *rs, int code, const char *out, int len, const char *headers) {
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":
@ -187,8 +187,12 @@ R_API ut8 *r_socket_http_handle_upload(const ut8 *str, int len, int *retlen) {
return NULL;
}
R_API void r_socket_http_close(RSocketHTTPRequest *rs) {
r_socket_close (rs->s);
}
/* close client socket and free struct */
R_API void r_socket_http_close (RSocketHTTPRequest *rs) {
R_API void r_socket_http_free(RSocketHTTPRequest *rs) {
r_socket_free (rs->s);
free (rs->path);
free (rs->host);
@ -227,6 +231,7 @@ int main() {
r_socket_http_response (rs, 404, "Invalid protocol");
}
r_socket_http_close (rs);
r_socket_http_free (rs);
}
}
#endif