void RequestParser::serveIndex() { QHttpResponseHeader header; header.setStatusLine(200, "OK", 1,1); QByteArray buffer = "<HTML><BODY>Index</BODY></HTML>"; header.setContentLength(buffer.length()); header.setContentType("text/html"); emit write(header.toString().toUtf8()); emit write(buffer); exit(0); }
void AcsMessageBuilder::buildHttpHeaderResponse(QHttpResponseHeader &responseHeader, int iContentLength, int code, const QString& status) { responseHeader.setStatusLine(code, status); responseHeader.setContentLength(iContentLength); if(iContentLength == 0){ responseHeader.setContentType("text/plain"); } else { responseHeader.setContentType("text/xml; charset=\"utf-8\""); } return; }
/** This method will ... * @param [inout] reqData: ... @param [out] error: error description for an invalid request * @return: can we serve this request? *******************************************************************************/ std::string CuteHttpParser::makeOkResp(CuteSrvRequest& reqData, const std::string& respStr) { QHttpResponseHeader ok; ok.setStatusLine(200); ok.addValue("Content-Type", "text/html"); ok.setContentLength(respStr.size()); string resp = ok.toString().toLatin1().constData();; // add content resp.append(respStr); resp.append(c_httpLineEnd); return resp; }
void RequestParser::serveFile(QString filename) { QFile file(filename); QHttpResponseHeader header; if (file.open(QIODevice::ReadOnly)) { header.setStatusLine(200, "OK", 1,0); QByteArray data = file.readAll(); header.setContentLength(data.length()); emit write(header.toString().toUtf8()); emit write("\r\n"); emit write(data); emit close(); } else { header.setStatusLine(404, "NOT FOUND", 1,0); emit write(header.toString().toUtf8()); emit close(); } }
/** This method will ... * @param [inout] reqData: ... @param [out] error: error description for an invalid request * @return: can we serve this request? *******************************************************************************/ std::string CuteHttpParser::makeErrorResp(CuteSrvRequest& reqData, int errCode, const std::string& errStr) { // format err string: // OPEN TODO:: error pages??? string errPage("<html><body><h2>"); errPage.append(errStr); errPage.append("</h2></body></html>"); QHttpResponseHeader err; err.setStatusLine(errCode, ""); err.addValue("Content-Type", "text/html"); err.setContentLength(errPage.size()); string resp = err.toString().toLatin1().constData(); // add content resp.append(errPage); resp.append(c_httpLineEnd); return resp; }