~george-edison55/jethttp/jethttp-singlethread

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
//=================================================
//
//             JetHTTP - CPHPOutput.cpp
//           Copyright 2011 - Nathan Osman
//     This file is released under the GPL v3
//
//=================================================

#include <CPHPOutput.h>

void CPHPOutput::Invoke(QProcessEnvironment env, QString resource_path, CHeaderManager &)
{
    QProcess process;

    process.setProcessEnvironment(env);
    process.setProcessChannelMode(QProcess::MergedChannels);

    process.start(QObject::tr("php-cgi %1").arg(resource_path), QProcess::ReadWrite);

    process.write("GET / HTTP/1.1\r\n\r\n");
    process.waitForBytesWritten();
    process.closeWriteChannel();

    process.waitForFinished();

    QByteArray output = process.readAll();

    // TODO: this part needs some help

    // We need to split the output data into two
    // parts - the new headers, and the content
    int index = output.indexOf("\r\n\r\n");

    if(index == -1)
        m_data = output;
    else
    {
        // Split the output into two chunks
        QString headers = output.mid(0, index);
        QByteArray content = output.mid(index + 4);

        // Process the headers
        QStringList lines = headers.split("\r\n");
        QString line;

        foreach(line,lines)
        {
            // Break apart the line
            QStringList line_parts = line.split(":");

            m_output_headers.SetHeader(line_parts.at(0), line_parts.at(1).trimmed());
        }

        m_data = content;
    }
}