Beispiel #1
0
void serve_wsgi(int fd, char *uri) 
{
    char buf[MAXLINE], body[MAXLINE];
    int len;
    printf("serving wsgi!\n");
    Response response;
    call_app(&response, &body, &len);
    
    if (response.status != NULL) {
        printf("%s\n", PyString_AsString(response.status));
    } else {
        printf("response->status is NULL");
        return;
    }
    sprintf(buf, "HTTP/1.0 %s \r\n", PyString_AsString(response.status));
    rio_writen(fd, buf, strlen(buf));
    sprintf(buf, "Server: toyws\r\n");
    rio_writen(fd, buf, strlen(buf));
    sprintf(buf, "Content-type: text/html\r\n");
    rio_writen(fd, buf, strlen(buf));
    sprintf(buf, "Content-length: %d\r\n\r\n", len);
    rio_writen(fd, buf, strlen(buf));

    rio_writen(fd, body, len);
}
Beispiel #2
0
void AppReply::parseResponse()
{
    content = call_app(method, url, body);
    offset = 0;

    open(ReadOnly | Unbuffered);

    // default attribute
    setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 200);
    setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/html; charset=UTF-8"));

    // TODO speed optimization needed
    int index;
    bool ok;
    int code;
    QByteArray line, key, value;
    QList<QByteArray> words;
    if ((index = content.indexOf("\r\n")) < 0) {
        goto finish;
    }
    line = content.left(index);
    words = line.split(' ');
    if (words.size() < 3) {
        goto finish;
    }
    code = words[1].toInt(&ok);
    if (!ok) {
        goto finish;
    }
    setAttribute(QNetworkRequest::HttpStatusCodeAttribute, code);
    offset = index + 2;

    // iterate over lines of header
    for(; (index = content.indexOf("\r\n", offset)) > offset; offset=index+2) {
        line = content.mid(offset, index-offset);
        int pos = line.indexOf(':');
        if (pos <= 0) {
            continue;
        }
        key = line.left(pos);
        value = line.mid(pos+1).trimmed();
        setRawHeader(key, value);
    }

    if (index == offset) {
        // the CRLF seperate headers and body
        offset += 2;
    }

finish:
//        qDebug() << "body:" << content.mid(offset);
    emit readyRead();
    emit finished();
}