File: execute.cc

package info (click to toggle)
crossroads 2.65-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,664 kB
  • ctags: 355
  • sloc: cpp: 4,212; perl: 1,658; xml: 269; makefile: 186; sh: 46
file content (66 lines) | stat: -rw-r--r-- 1,873 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "webinterface"
#include "balancer/balancer"

void Webinterface::execute() {
    Threadlist::desc("Web interface");

    // Create the server socket, or retry infinitely.
    // This is maybe a too big precaution - previous xr's are responsible
    // for killing off their web interfaces. But we don't want a new xr
    // start to croak and cause downtime just because the web interface,
    // so we just retry for a bit.
    while (true) {
	try {
	    msg ("Starting web interface\n");
	    sfd = serversocket (config.webinterfaceip(),
				config.webinterfaceport(),
				"web interface", Servertype::t_tcp);
	} catch (Error const &e) {
	    cerr << e.what() << "(webinterface, retrying in a sec)\n";
	    sleep (1);
	    continue;
	}
	break;
    }

    msg ((Mstr("Web interface started on socket ") + sfd) + "\n");
    while (!balancer.terminate()) {
	try {
	    Fdset fdset(0);
	    fdset.add (sfd);
	    fdset.wait_r();
	    if (fdset.readable(sfd)) {
		int size;
		struct sockaddr_in clname;
		if ( (cfd = accept (sfd, (struct sockaddr *) &clname,
				    (socklen_t *)&size)) < 0 )
		    warnmsg(Mstr("Web interface: failed to accept "
				 "network connection: ") +
			    Mstr(strerror(errno)) + "\n");
		else {
		    serve ();
		    socketclose(cfd);
		}
	    }
	} catch (Error const &e) {
	    cerr <<  e.what() << " (webinterface)\n";
	    ostringstream m;
	    m <<
		"<h1>Web interface error</h1>\n"
		"XR's web interface could not handle your request.<p/>\n"
		"<i>" << e.what() << "</i>\n";
	    ostringstream o;
	    o <<
		"HTTP/1.0 500 Server Error\r\n"
		"X-Reason: " << e.what() << "\r\n"
		"Content-Length: " << m.str().length() << "\r\n"
		"\r\n" <<
		m.str();
	    Netbuffer buf(o.str());
	    buf.netwrite(cfd, config.client_write_timeout());
	    socketclose(cfd);
	}
    }
    msg ("Web interface stopping.\n");
    socketclose(sfd);
}