head	1.5;
access;
symbols
	tonic_4:1.5
	tonic_3:1.5
	tonic_2:1.5
	demo:1.5.0.2
	design_report:1.5
	dec2:1.1.1.1
	oral_presentation_freeze:1.1.1.1.0.6
	DAVEG_CDP:1.1.1.1.0.4
	template_integ:1.1.1.1.0.2
	v1_1:1.1.1.1
	nov19:1.1.1.1
	course_staff:1.1.1;
locks; strict;
comment	@ * @;


1.5
date	95.12.11.07.11.53;	author hartmans;	state Exp;
branches;
next	1.4;

1.4
date	95.12.07.11.08.01;	author hartmans;	state Exp;
branches;
next	1.3;

1.3
date	95.12.07.09.24.04;	author hartmans;	state Exp;
branches;
next	1.2;

1.2
date	95.12.05.10.44.44;	author hartmans;	state Exp;
branches;
next	1.1;

1.1
date	95.11.19.05.50.04;	author hartmans;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	95.11.19.05.50.05;	author hartmans;	state Exp;
branches;
next	;


desc
@@


1.5
log
@Add safeify_filename.
Try to fix streams handling under rs6k one last time;
failed.
@
text
@#include <6170.h>
#include "ps5/http_server.h"
#include "ps5/http_response.h"
#include "ps5/error_response.h"
#include "ps5/server.h"


#include <strstream.h>
#include <unistd.h>
#include <ctype.h>

http_server::http_server(int port_, web_node *root__) {
    serv = new server(port_);
    root_ = root__;
    debugging = false;
}

static string trim_crud(string x) {
// remove leading and trailing whitespace
    int l = 0, r = x.length() - 1;
    while (l <= r && isspace(x[l])) l++;
    while (r >= l && isspace(x[r])) r--;
    return x.substring(l, r - l + 1);
}

void http_server::handle_one() {
    int s = serv->accept();
    fstream st = s;
    istream &in = st;
    ostream &out = st;
    bool http_10 = false;

    string method, url, proto;
    extensions *ext = new extensions;

    if ((in >> method ))
        {
	    
	    if ((method == "GET" || method == "POST") &&
	in >> url &&
	url[0] == '/') {
	if (!read_line(in, proto)) goto close;
    } else goto close;
	}
    else goto close;
    if (debugging) {
	cerr << method << ' ' << url << proto << '\n';
    }
    proto = trim_crud(proto);
    if (proto == "HTTP/1.0") {
	http_10 = true;
	// skip over headers
	string hdr, val;
        for (;;) {
	    if (!read_line(in, hdr)) goto close;
	    if (hdr == "\r" || hdr.length() == 0) break;
	    int index;
	    if (!hdr.find_char(':', 0, index)) {
		strstream s;
		s << "Invalid header line: \"" << trim_crud(hdr) << "\"\n";
		if (debugging) cerr << s.str();
	        error_response(http_response::BAD_REQUEST, s.str()).send(out);
		goto close;
	    }
	    string n = hdr.substring(0, index);
	    index++;
	    if (index < hdr.length()) {
		val = trim_crud(hdr.substring(index, hdr.length() - index));
	    } else {
		val = "";
	    }
	    string oldval;
	    if (ext->find(n, oldval)) {
		ext->store(n, oldval + " " + val);
	    } else {
		ext->store(n, val);
	    }
	    if (debugging) cerr << hdr << '\n';
	}
    } else if (proto == "") {
	http_10 = false;
    } else  {
	strstream s;
	s << "<b>Don't know how to handle protocol \"" << proto << "\"</b>\n";
	error_response(http_response::NOT_IMPLEMENTED, s.str()).send(out);
	if (debugging) cerr << s.str();
	goto close;
    }
    {
	http_response *hr = root_->get(url, ext);
	if (http_10) hr->send(out);
	else hr->body(out);
	if (debugging) cerr << hr->code() << ' ' <<
	    result_description(hr->code()) << '\n';
    }
close:
    out.flush();
    st.close();
    
    
}

int http_server::file_descriptor() {
    return serv->file_descriptor();
}

void http_server::close() {
    serv->close();
}

void http_server::debug(bool d) {
    debugging = d;
}
@


1.4
log
@Fix URI handling bugs:
unescape_uri forgot to initialize i,
several bugs with finding_value in parse_query
@
text
@d28 3
a30 2
    ifstream in = s;
    ofstream out = s;
d98 1
a98 2
    in.close();
    out.close();
d100 1
@


1.3
log
@random changes I made and forgot about
@
text
@d35 1
a35 1
    if (!(in >> method ))
@


1.2
log
@Fix potential bug
@
text
@d1 1
d5 1
a5 2
#include "ps4/server.h"
#include "ps4/sockstream.h"
d7 1
d27 3
a29 3
    sockstream s = serv->accept();
    ifstream &in = s.in();
    ofstream &out = s.out();
d35 4
a38 2
    if (in >> method &&
	(method == "GET" || method == "POST") &&
d43 2
d97 3
a99 1
    s.close();
@


1.1
log
@Initial revision
@
text
@d27 2
a28 2
    istream &in = s.in();
    ostream &out = s.out();
@


1.1.1.1
log
@Bring web_server code into our tree
@
text
@@
