Skip to content

Commit

Permalink
Initial implementation of the RSocketHTTPServer
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Sep 5, 2012
1 parent e2af4c9 commit de519e7
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 9 deletions.
11 changes: 5 additions & 6 deletions libr/core/cmd_search.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,14 @@ static inline void print_search_progress(ut64 at, ut64 to, int n) {
}

static int cmd_search(void *data, const char *input) {
const char *mode;
char *inp;
RCore *core = (RCore *)data;
ut64 at, from, to;
//RIOSection *section;
int i, len, ret, dosearch = R_FALSE;
int inverse = R_FALSE;
RCore *core = (RCore *)data;
int aes_search = R_FALSE;
int ignorecase = R_FALSE;
int inverse = R_FALSE;
ut64 at, from, to;
const char *mode;
char *inp;
ut64 n64;
ut32 n32;
ut16 n16;
Expand Down
2 changes: 2 additions & 0 deletions libr/include/r_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ typedef struct { } RSystem;
typedef struct { } RLog;
#define RStr char*

typedef int (*RStrRangeCallback) (void *, int);

typedef struct r_mem_pool_t {
ut8 **nodes;
int ncount;
Expand Down
5 changes: 4 additions & 1 deletion libr/socket/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ include ../config.mk

NAME=r_socket
#DEPS=r_util
OBJ=socket.o proc.o http.o
OBJ=socket.o proc.o http.o http_server.o
ifeq (${HAVE_LIB_SSL},1)
CFLAGS+=${SSL_CFLAGS}
LDFLAGS+=${SSL_LDFLAGS}
Expand All @@ -25,3 +25,6 @@ LDFLAGS=-lwsock32
endif

include ../rules.mk

test:
gcc -DMAIN -g -ggdb *.c -I ../include/ `pkg-config openssl --libs`
2 changes: 1 addition & 1 deletion libr/socket/http.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2011 pancake<nopcode.org> */
/* radare - LGPL - Copyright 2011-2012 - pancake */

#include <r_socket.h>

Expand Down
89 changes: 89 additions & 0 deletions libr/socket/http_server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* radare - LGPL - Copyright 2012 - pancake */

#include <r_socket.h>

typedef struct r_socket_http_request {
RSocket *s;
char *path;
char *host;
char *agent;
char *method;
char *data;
} RSocketHTTPRequest;

static RSocketHTTPRequest *r_socket_http_accept (RSocket *s) {
int pxx = 1, first = 0;
char buf[1024], *p, *q;
RSocketHTTPRequest *hr = R_NEW0 (RSocketHTTPRequest);
hr->s = r_socket_accept (s);
if (!hr->s) {
free (hr);
return NULL;
}
for (;;) {
int xx = r_socket_gets (hr->s, buf, sizeof (buf));
int yy = r_socket_ready (hr->s, 0, 20);
eprintf ("READ %d (%s) READY %d\n", xx, buf, yy);
if (yy == 0) {
eprintf ("BREAK\n");
break;
}
if (xx == 0 && pxx == 0) {
eprintf ("BREAK LONG\n");
break;
}
pxx = xx;

if (first==0) {
first = 1;
p = strchr (buf, ' ');
if (p) *p = 0;
hr->method = strdup (buf);
if (p) {
q = strchr (p+1, ' ');
if (q) *q = 0;
hr->path = strdup (p+1);
}
}
}
eprintf ("RET\n");

return hr;
}

static void r_socket_http_response (RSocketHTTPRequest *rs, int code, const char *out) {
// HTTP/1.1 200 OK
// headers
// \n\n
// body
r_socket_puts (rs->s, "HTTP/1.1 200 OK\n\nHello World\n");
}

/* close client socket and free struct */
static void r_socket_http_close (RSocketHTTPRequest *rs) {
r_socket_free (rs->s);
free (rs->path);
free (rs->host);
free (rs->agent);
free (rs->method);
free (rs->data);
free (rs);
}

#if MAIN
int main() {
RSocket *s = r_socket_new (R_FALSE);
if (!r_socket_listen (s, "8080", NULL)) {
eprintf ("Cannot listen here\n");
return 1;
}
for (;;) {
RSocketHTTPRequest *rs = r_socket_http_accept (s);
if (!strcmp (rs->method, "GET")) {
// get method
r_socket_http_response (rs, 200, "Fuck yeah");
r_socket_http_close (rs);
}
}
}
#endif
23 changes: 22 additions & 1 deletion libr/util/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ R_API char *r_str_newf(const char *fmt, ...) {
char string[1024];
va_list ap;
va_start (ap, fmt);
vsnprintf (string, 1023, fmt, ap);
vsnprintf (string, sizeof (string), fmt, ap);
fmt = r_str_new (string);
va_end (ap);
return (char*)fmt;
Expand Down Expand Up @@ -923,3 +923,24 @@ R_API const char *r_str_casestr(const char *a, const char *b) {
R_API int r_str_write (int fd, const char *b) {
return write (fd, b, strlen (b));
}

R_API void r_str_range_foreach(const char *r, RStrRangeCallback cb, void *u) {
const char *p = r;
for (; *r; r++) {
if (*r == ',') {
cb (u, atoi (p));
p = r+1;
}
if (*r == '-') {
if (p != r) {
int from = atoi (p);
int to = atoi (r+1);
for (; from<=to; from++)
cb (u, from);
} else fprintf (stderr, "Invalid range\n");
for (r++; *r && *r!=','&& *r!='-'; r++);
p = r;
}
}
if (*p) cb (u, atoi (p));
}

0 comments on commit de519e7

Please sign in to comment.