Skip to content

Commit

Permalink
Push remote host into input.c.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristaps committed Nov 23, 2014
1 parent 90578e3 commit b6a13e1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 47 deletions.
30 changes: 0 additions & 30 deletions index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,6 @@
<i>Version @VERSION@</i> is a minor-number bump due to a significantly changing API.
Not much has changed in the back-end of the system (with one significant exception!); most of these changes are organisational.
</p>
<ul>
<li>
Add a <a href="http://www.cl.cam.ac.uk/research/security/capsicum/freebsd.html">Capsicum</a>
sandbox on <a href="http://www.freebsd.org">FreeBSD</a>.
</li>
<li>
Split <span class="file">libkcgi</span>, <a href="kcgi.3.html">kcgi(3)</a>, into
<span class="file">libkcgihtml</span>, <a href="kcgihtml.3.html">kcgihtml(3)</a>.
This simplifies the system dramatically and allows for more extensions (e.g., JSON) outside of
the main body of code.
</li>
<li>
Split <a href="kcgi.3.html">kcgi(3)</a> into per-function manpages, e.g., <a
href="khttp_parse.3.html">khttp_parse(3)</a>.
This makes system reading documentation much easier.
</li>
<li>
Merge small fix to escape quotes in HTML text and <a
href="kvalid_string.3.html">kvalid_date(3)</a> month calculator.
</li>
<li>
Add <a href="khttp_free.3.html">khttp_child_free(3)</a> for closing the HTTP in a child of the
CGI process.
This is useful if your web application forks to perform some long-running function (e.g.,
mailing).
</li>
<li>
Add a few more commonly-used media types.
</li>
</p>
</section>
</header>
<article>
Expand Down
21 changes: 16 additions & 5 deletions input.c
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,9 @@ khttp_input_parent(int fd, struct kreq *r, pid_t pid)
} else if (fullread(fd, &r->auth, sizeof(enum kauth), 0) < 0) {
XWARNX("failed to read authorisation type");
goto out;
} if (fullreadword(fd, &r->remote) < 0) {
XWARNX("failed to read fullpath");
goto out;
} if (fullreadword(fd, &r->fullpath) < 0) {
XWARNX("failed to read fullpath");
goto out;
Expand Down Expand Up @@ -1073,6 +1076,7 @@ khttp_input_child(int fd, const struct kvalid *keys,
{
struct parms pp;
char *cp, *ep, *sub;
const char *ccp;
enum kmethod meth;
enum kauth auth;
size_t len;
Expand All @@ -1086,24 +1090,31 @@ khttp_input_child(int fd, const struct kvalid *keys,
/* RFC 3875, 4.1.12. */
/* We assume GET if not supplied. */
meth = KMETHOD_GET;
if (NULL != (cp = getenv("REQUEST_METHOD")))
if (NULL != (ccp = getenv("REQUEST_METHOD")))
for (meth = 0; meth < KMETHOD__MAX; meth++)
if (0 == strcmp(kmethods[meth], cp))
if (0 == strcmp(kmethods[meth], ccp))
break;
fullwrite(fd, &meth, sizeof(enum kmethod));

/* Determine authenticaiton: RFC 3875, 4.1.1. */
auth = KAUTH_NONE;
if (NULL != (cp = getenv("AUTH_TYPE")))
if (NULL != (ccp = getenv("AUTH_TYPE")))
for (auth = 0; auth < KAUTH_UNKNOWN; auth++) {
if (NULL == kauths[auth])
continue;
if (0 == strcmp(kauths[auth], cp))
if (0 == strcmp(kauths[auth], ccp))
break;
}

fullwrite(fd, &auth, sizeof(enum kauth));

/* RFC 3875, 4.1.8. */
/* Never supposed to be NULL, but to be sure... */
if (NULL == (ccp = getenv("REMOTE_ADDR")))
ccp = "127.0.0.1";
len = strlen(ccp);
fullwrite(fd, &len, sizeof(size_t));
fullwrite(fd, ccp, len);

/*
* Now parse the first path element (the page we want to
* access), subsequent path information, and the file suffix.
Expand Down
10 changes: 0 additions & 10 deletions kcgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -756,16 +756,6 @@ khttp_parsex(struct kreq *req,
if (NULL == req->pname)
goto err;

/* RFC 3875, 4.1.8. */
/* Never supposed to be NULL, but to be sure... */
if (NULL == (cp = getenv("REMOTE_ADDR")))
req->remote = XSTRDUP("127.0.0.1");
else
req->remote = XSTRDUP(cp);

if (NULL == req->remote)
goto err;

/* Never supposed to be NULL, but to be sure... */
if (NULL == (cp = getenv("HTTP_HOST")))
req->host = XSTRDUP("localhost");
Expand Down
7 changes: 6 additions & 1 deletion sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ enum key {
enum templ {
TEMPL_TITLE,
TEMPL_NAME,
TEMPL_REMOTE_ADDR,
TEMPL__MAX
};

Expand Down Expand Up @@ -87,7 +88,8 @@ static const struct kvalid keys[KEY__MAX] = {
*/
static const char *const templs[TEMPL__MAX] = {
"title", /* TEMPL_TITLE */
"name" /* TEMPL_NAME */
"name", /* TEMPL_NAME */
"remote_addr", /* TEMPL_REMOTE_ADDR */
};

/*
Expand Down Expand Up @@ -128,6 +130,9 @@ template(size_t key, void *arg)
case (TEMPL_NAME):
khtml_text(req, "name");
break;
case (TEMPL_REMOTE_ADDR):
khtml_text(req, req->req->remote);
break;
default:
abort();
}
Expand Down
2 changes: 1 addition & 1 deletion template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<h1>@@name@@</h1>
</header>
<article>
Hello, world.
Hello, @@remote_addr@@.
</article>
</body>
</html>

0 comments on commit b6a13e1

Please sign in to comment.