Add support for http response headers

Fix some issues in the www ui
This commit is contained in:
pancake 2012-10-03 12:10:18 +02:00
parent f74b74ab6c
commit 0aa55a4e01
5 changed files with 18 additions and 14 deletions

View File

@ -71,9 +71,9 @@ R_API int r_core_rtr_http(RCore *core, int launch) {
out = r_core_cmd_str_pipe (core, cmd);
if (out) {
r_str_unescape (out);
r_socket_http_response (rs, 200, out, 0);
r_socket_http_response (rs, 200, out, 0, "Content-Type: text/plain\n");
free (out);
} else r_socket_http_response (rs, 200, "oops", 0);
} else r_socket_http_response (rs, 200, "oops", 0, NULL);
} else {
const char *root = r_config_get (core->config, "http.root");
char path[1024];
@ -85,12 +85,12 @@ R_API int r_core_rtr_http(RCore *core, int launch) {
int sz = 0;
char *f = r_file_slurp (path, &sz);
if (f) {
r_socket_http_response (rs, 200, f, sz);
r_socket_http_response (rs, 200, f, sz, NULL);
free (f);
} else r_socket_http_response (rs, 403, "Permission denied", 0);
} else r_socket_http_response (rs, 403, "Permission denied", 0, NULL);
} else {
// TODO: directory listing?
r_socket_http_response (rs, 404, "File not found\n", 0);
r_socket_http_response (rs, 404, "File not found\n", 0, NULL);
}
}
#if 0
@ -100,11 +100,11 @@ R_API int r_core_rtr_http(RCore *core, int launch) {
strcpy (buf, "<html><body><h2>XSS test</h2>\n");
r_str_unescape ((char *)rs->data);
strcat (buf, (char*)rs->data);
r_socket_http_response (rs, 200, buf, 0);
r_socket_http_response (rs, 200, buf, 0, NULL);
free (buf);
#endif
} else {
r_socket_http_response (rs, 404, "Invalid protocol", 0);
r_socket_http_response (rs, 404, "Invalid protocol", 0, NULL);
}
r_socket_http_close (rs);
}

View File

@ -81,7 +81,7 @@ typedef struct r_socket_http_request {
} RSocketHTTPRequest;
R_API RSocketHTTPRequest *r_socket_http_accept (RSocket *s);
R_API void r_socket_http_response (RSocketHTTPRequest *rs, int code, const char *out, int x);
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);
#endif
#endif

View File

@ -58,14 +58,14 @@ R_API RSocketHTTPRequest *r_socket_http_accept (RSocket *s) {
return hr;
}
R_API void r_socket_http_response (RSocketHTTPRequest *rs, int code, const char *out, int len) {
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==404?"NOT FOUND":
"UNKNOWN";
if (len<1) len = strlen (out);
r_socket_printf (rs->s, "HTTP/1.0 %d %s\n"
"Content-Length: %d\n\n", code, strcode, len);
r_socket_printf (rs->s, "HTTP/1.0 %d %s\n%s"
"Content-Length: %d\n\n", code, strcode, headers, len);
r_socket_write (rs->s, (void*)out, len);
}

View File

@ -13,8 +13,11 @@
function Ajax (method, uri, body, fn) {
var x = new XMLHttpRequest ();
x.open (method, uri, false);
x.setRequestHeader ('Accept', 'text/plain');
x.onreadystatechange = function (y) {
if (fn) fn (x.responseText);
if (x.status == 200) {
if (fn) fn (x.responseText);
} else alert ("ajax "+x.status)
}
x.send (body);
}
@ -400,13 +403,14 @@ Enjoy!
</div>
<div class="console" style="top:25px !important; position:absolute;z-index:-99">
<div class="console" style="top:32px !important; position:absolute;z-index:-99">
<div id="less">
<a href='javascript:less()'>... prev</a>
</div>
<p id="output"></p>
<div id="more">
<a href='javascript:more()'>... next</a>
<br /><br /><br />
</div>
</div>
</body>

View File

@ -2,7 +2,7 @@ body {
background-color: black;
font-family: Verdana;
border:0px;
width:320px;
height: 0px;
margin:10px;
}