]> rtime.felk.cvut.cz Git - hydro.git/blob - app-bohyn/src/http.cc
Added monitoring web system. Minor changes in regulator and in control.
[hydro.git] / app-bohyn / src / http.cc
1
2 #include "http.h"
3
4 QString CRLF("\r\n");
5
6 /* class UploadedFile */
7 const UploadedFile &UploadedFile::sharedNull() {
8     static UploadedFile uf = UploadedFile(DUMMY_TYPE());
9     return uf;
10 }
11
12 UploadedFile::UploadedFile() {
13     *this = sharedNull();
14 }
15
16 UploadedFile::UploadedFile(QString _file_name, QString _content_type, int _file_size, const QByteArray &_content) {
17     d = new Data;
18     d->null = false;
19     d->file_name = _file_name;
20     d->content_type = _content_type;
21     d->file_size = _file_size;
22     d->content = _content;
23 }
24
25
26 /* class HttpRequest */
27 void HttpRequest::parsePostData() {
28     QList<QPair<QByteArray, QByteArray> > queryItems;
29     QString content_type(getenv("CONTENT_TYPE"));
30     int content_length = QString(getenv("CONTENT_LENGTH")).toInt();
31     if (content_type.startsWith("multipart/form-data")) {
32         parseMultipartData(content_length, content_type);
33     } else {
34         if (content_length > 0) {
35             QByteArray buf_in;
36             buf_in.resize(content_length);
37             fread(buf_in.data(), 1, buf_in.size(), FCGI_stdin);
38             QUrl pseudoUrl(QString("/?").append(buf_in));
39             queryItems = pseudoUrl.encodedQueryItems();
40             for (int i = 0; i < queryItems.size(); i++) {
41                 QString first = QUrl::fromPercentEncoding(queryItems[i].first).replace("+", "%20");
42                 QString second = QUrl::fromPercentEncoding(queryItems[i].second).replace("+", "%20");
43                 post_data.insert(QUrl::fromPercentEncoding(first.toUtf8()), QUrl::fromPercentEncoding(second.toUtf8()));
44             }
45         }
46     }
47 }
48
49 void HttpRequest::parseMultipartData(int content_length, QString content_type) {
50     QByteArray buf_in;
51     buf_in.resize(content_length);
52     if (fread(buf_in.data(), 1, buf_in.size(), FCGI_stdin) != (uint)content_length)
53         return;
54     QByteArray boundary_str = QString(content_type).mid(QString("multipart/form-data").size() + QString("; boundary=").size()).prepend("--").toAscii();
55     buf_in = buf_in.mid(boundary_str.size() + CRLF.size());
56     QList<QByteArray> content_parts;
57     int boundary_index;
58     while (true) {
59         boundary_index = buf_in.indexOf(boundary_str);
60         if (boundary_index == -1)
61             break;
62         content_parts.append(buf_in.left(boundary_index - CRLF.size()));
63         buf_in = buf_in.mid(boundary_index + boundary_str.size() + CRLF.size());
64     }
65     buf_in.clear();
66     
67     for (int i = 0; i < content_parts.size(); i++)
68         parseContentPart(content_parts[i]);
69 }
70
71 void HttpRequest::parseContentPart(QByteArray &content_part) {
72     int crlf_pos = content_part.indexOf(CRLF);
73     QRegExp file_reg("^Content-Disposition: form-data; name=\"(.+)\"; filename=\"(.*)\"$");
74     QRegExp field_reg("^Content-Disposition: form-data; name=\"(.+)\"$");
75     if (file_reg.exactMatch(content_part.left(crlf_pos))) {
76         parseFileContent(file_reg.cap(1), file_reg.cap(2), content_part, crlf_pos + CRLF.size());
77     } else if (field_reg.exactMatch(content_part.left(crlf_pos))) {
78         parseFieldContent(field_reg.cap(1), content_part, crlf_pos + CRLF.size());
79     } else
80         return;
81 }
82
83 void HttpRequest::parseFileContent(QString field_name, QString file_name, QByteArray &content_part, int start_pos) {
84     int crlf_pos = content_part.indexOf(CRLF, start_pos);
85     QRegExp content_type_reg("^Content-Type: (.+)$");
86     if (!content_type_reg.exactMatch(content_part.mid(start_pos, crlf_pos - start_pos)))
87         return;
88     QString content_type = content_type_reg.cap(1);
89     content_part = content_part.mid(crlf_pos + 2 * CRLF.size(), content_part.size() - crlf_pos - 2 * CRLF.size());
90     if (file_name != "") {
91 //         logToFile(QString("vytvarim soubor %1, size %2").arg(file_name).arg(content_part.size()));
92         files_data.insert(field_name, UploadedFile(file_name, content_type, content_part.size(), content_part));
93 //         logToFile(QString("vytvoren soubor %1").arg(file_name));
94     }
95 }
96
97 void HttpRequest::parseFieldContent(QString field_name, QByteArray &content_part, int start_pos) {
98     QString content = content_part.mid(start_pos + CRLF.size(), content_part.size() - start_pos - CRLF.size());
99     post_data.insert(field_name, content);
100 }
101
102
103 HttpRequest::HttpRequest() {
104     QUrl requestUrl(getenv("REQUEST_URI"));
105     _path = requestUrl.path();
106     _method = getenv("REQUEST_METHOD");
107     QList<QPair<QByteArray, QByteArray> > queryItems = requestUrl.encodedQueryItems();
108     for (int i = 0; i < queryItems.size(); i++) {
109         QString first = QUrl::fromPercentEncoding(queryItems[i].first);
110         QString second = QUrl::fromPercentEncoding(queryItems[i].second);
111         get_data.insert(QUrl::fromPercentEncoding(first.toUtf8()), QUrl::fromPercentEncoding(second.toUtf8()));
112     }
113     parsePostData();
114
115 //     QMapIterator<QString, QString> it(post_data);
116 //     while (it.hasNext()) {
117 //         it.next();
118 //         logToFile(QString("POST --- KEY: %1, VAL %2").arg(it.key(), it.value()));
119 //     }
120 //     QMapIterator<QString, UploadedFile> itf(files_data);
121 //     while (itf.hasNext()) {
122 //         itf.next();
123 //         logToFile(QString("FILES --- KEY: %1, VAL %2, SIZE %3").arg(itf.key(), itf.value().fileName()).arg(itf.value().size()));
124 //         logToFile(QString("CCCCCCC\n%1").arg(QString(itf.value().contentRef())));
125 //     }
126 }