mirror of https://github.com/vitalif/phantomjs
very basic response + request handling for WebServer
the api is very limited but already makes the server.js example do something useful with the server1.4
parent
73705e484b
commit
0abdc7f9ed
|
@ -7,17 +7,23 @@ if (phantom.args.length !== 1) {
|
||||||
phantom.exit();
|
phantom.exit();
|
||||||
} else {
|
} else {
|
||||||
port = phantom.args[0];
|
port = phantom.args[0];
|
||||||
server.listen(port, function () {
|
server.listen(port, function (request, response) {
|
||||||
console.log("GOT HTTP REQUEST");
|
console.log("GOT HTTP REQUEST: url=" + request.url() + ", method: " + request.method());
|
||||||
|
var headers = "HTTP/1.1 200 OK\r\n" +
|
||||||
|
"Cache: no-cache\r\n" +
|
||||||
|
"Content-Type: text/html\r\n" +
|
||||||
|
"\r\n";
|
||||||
|
response.writeHeaders(headers);
|
||||||
|
var contents = "<html><head><title>YES!</title></head>" +
|
||||||
|
"<body><p>pretty cool :)</body></html>";
|
||||||
|
response.writeBody(contents);
|
||||||
});
|
});
|
||||||
console.log("http://localhost:" + port + "/");
|
console.log("http://localhost:" + port + "/");
|
||||||
page.open("http://localhost:" + port + "/", function (status) {
|
page.open("http://localhost:" + port + "/", function (status) {
|
||||||
if (status !== 'success') {
|
if (status !== 'success') {
|
||||||
console.log('FAIL to load the address');
|
console.log('FAIL to load the address');
|
||||||
} else {
|
} else {
|
||||||
console.log('Page title is ' + page.evaluate(function () {
|
console.log(page.content);
|
||||||
return document.title;
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
phantom.exit();
|
phantom.exit();
|
||||||
});
|
});
|
||||||
|
|
|
@ -44,7 +44,8 @@ static void *callback(mg_event event,
|
||||||
// TODO: check whether direct call works as well
|
// TODO: check whether direct call works as well
|
||||||
bool handled = false;
|
bool handled = false;
|
||||||
QMetaObject::invokeMethod(server, "handleRequest", Qt::BlockingQueuedConnection,
|
QMetaObject::invokeMethod(server, "handleRequest", Qt::BlockingQueuedConnection,
|
||||||
Q_ARG(mg_event, event), Q_ARG(const mg_request_info*, request_info),
|
Q_ARG(mg_event, event), Q_ARG(mg_connection*, conn),
|
||||||
|
Q_ARG(const mg_request_info*, request_info),
|
||||||
Q_ARG(bool*, &handled));
|
Q_ARG(bool*, &handled));
|
||||||
qWarning() << "handled:" << handled;
|
qWarning() << "handled:" << handled;
|
||||||
if (handled) {
|
if (handled) {
|
||||||
|
@ -85,12 +86,59 @@ void WebServer::listenOnPort(const QString& port)
|
||||||
qWarning() << "listening on port" << port << this;
|
qWarning() << "listening on port" << port << this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebServer::handleRequest(mg_event event, const mg_request_info *requestInfo, bool* handled)
|
void WebServer::handleRequest(mg_event event, mg_connection *conn, const mg_request_info *request,
|
||||||
|
bool *handled)
|
||||||
{
|
{
|
||||||
qWarning() << "GOT CALLBACK" << event << requestInfo << (QThread::currentThread() == thread());
|
Q_ASSERT(QThread::currentThread() == thread());
|
||||||
|
qWarning() << "GOT CALLBACK" << event << request;
|
||||||
if (event == MG_NEW_REQUEST) {
|
if (event == MG_NEW_REQUEST) {
|
||||||
emit newRequest();
|
WebServerRequest requestObj(request);
|
||||||
|
WebServerResponse responseObj(conn);
|
||||||
|
emit newRequest(&requestObj, &responseObj);
|
||||||
*handled = true;
|
*handled = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
*handled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//BEGIN WebServerRequest
|
||||||
|
WebServerRequest::WebServerRequest(const mg_request_info *request)
|
||||||
|
: m_request(request)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
QString WebServerRequest::method() const
|
||||||
|
{
|
||||||
|
///TODO: encoding?!
|
||||||
|
return QString::fromLocal8Bit(m_request->request_method);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString WebServerRequest::url() const
|
||||||
|
{
|
||||||
|
///TODO: encoding?!
|
||||||
|
return QString::fromLocal8Bit(m_request->uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
//END WebServerRequest
|
||||||
|
|
||||||
|
//BEGIN WebServerResponse
|
||||||
|
|
||||||
|
WebServerResponse::WebServerResponse(mg_connection *conn)
|
||||||
|
: m_conn(conn)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebServerResponse::writeHeaders(const QString &headers)
|
||||||
|
{
|
||||||
|
mg_printf(m_conn, "%s", qPrintable(headers));
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebServerResponse::writeBody(const QString &body)
|
||||||
|
{
|
||||||
|
mg_printf(m_conn, "%s", qPrintable(body));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//END WebServerResponse
|
||||||
|
|
|
@ -39,6 +39,9 @@
|
||||||
|
|
||||||
class Config;
|
class Config;
|
||||||
|
|
||||||
|
class WebServerRequest;
|
||||||
|
class WebServerResponse;
|
||||||
|
|
||||||
class WebServer : public QObject
|
class WebServer : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -50,14 +53,54 @@ public:
|
||||||
public slots:
|
public slots:
|
||||||
///WARNING: must not be the same name as in the javascript api...
|
///WARNING: must not be the same name as in the javascript api...
|
||||||
void listenOnPort(const QString &port);
|
void listenOnPort(const QString &port);
|
||||||
void handleRequest(mg_event event, const mg_request_info *request_info, bool* handled);
|
void handleRequest(mg_event event, mg_connection* conn, const mg_request_info* request,
|
||||||
|
bool* handled);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void newRequest();
|
void newRequest(QObject *request, QObject *response);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Config *m_config;
|
Config *m_config;
|
||||||
mg_context *m_ctx;
|
mg_context *m_ctx;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: copy more from http://nodejs.org/docs/latest/api/http.html#http.ServerRequest
|
||||||
|
*/
|
||||||
|
class WebServerRequest : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
WebServerRequest(const mg_request_info *request);
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
QString method() const;
|
||||||
|
QString url() const;
|
||||||
|
|
||||||
|
///TODO: extend API
|
||||||
|
private:
|
||||||
|
const mg_request_info *m_request;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: copy more from http://nodejs.org/docs/latest/api/http.html#http.ServerResponse
|
||||||
|
*/
|
||||||
|
class WebServerResponse : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
WebServerResponse(mg_connection *conn);
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
///TODO: improve API
|
||||||
|
void writeHeaders(const QString& headers);
|
||||||
|
///TODO: implictly write default headers if not called yet
|
||||||
|
void writeBody(const QString& body);
|
||||||
|
|
||||||
|
private:
|
||||||
|
mg_connection *m_conn;
|
||||||
|
};
|
||||||
|
|
||||||
#endif // WEBSERVER_H
|
#endif // WEBSERVER_H
|
||||||
|
|
Loading…
Reference in New Issue