Hobrasoft httpd server
Embedded HTTP server for Qt and C++
httpsettings.cpp
Go to the documentation of this file.
1 
7 #include "httpsettings.h"
8 #include <QSettings>
9 
68 int qHash(const QSslError error) {
69  return qHash(error.error());
70 }
71 
72 using namespace HobrasoftHttpd;
73 
74 
75 HttpSettings::HttpSettings(QObject *parent) : QObject(parent) {
76  setDefaultValues();
77 }
78 
79 
80 void HttpSettings::setDefaultValues() {
81  m_port = 8080;
82  m_address = QHostAddress::Any;
83  m_timeout = 600;
84  m_maxAge = 3600;
85  m_encoding = "UTF-8";
86  m_docroot = ".";
87  m_indexFile = "index.html";
88  m_sessionCookieName = "sessionid";
89  m_sessionExpirationTime = 3600;
90  m_maxRequestSize = 16384;
91  m_maxMultiPartSize = 16728064;
92  m_useSSL = false;
93  m_threads = false;
94 
95  m_default_section2 = "http";
96  m_default_port = 8080;
97  m_default_address = QHostAddress::Any;
98  m_default_timeout = 600;
99  m_default_maxAge = 3600;
100  m_default_encoding = "UTF-8";
101  m_default_docroot = ".";
102  m_default_indexFile = "index.html";
103  m_default_sessionCookieName = "sessionid";
104  m_default_sessionExpirationTime = 3600;
105  m_default_maxRequestSize = 16384;
106  m_default_maxMultiPartSize = 16728064;
107  m_default_useSSL = false;
108  m_default_sslKey = "";
109  m_default_sslCrt = "";
110  m_default_sslCaCrt = "";
111  m_default_ignoreAllSslErrors = true;
112  m_default_threads = true;
113  m_default_gzip = true;
114 }
115 
116 
117 void HttpSettings::readSettings(const QSettings *settings, const QString& section, const QString& section2) {
118  m_section2 = section2;
119  readSettings(settings, section);
120 }
121 
122 
123 void HttpSettings::readSettings(const QSettings *settings, const QString& section) {
124  m_port = settings->value( section + "/port",
125  settings->value(m_section2 + "/port", m_default_port)).toInt();
126  if (settings->contains(section+"/address")) {
127  m_address = settings->value(section+"/address").toString();
128  // qDebug() << "obsahuje sekce 1, nastavuji" << settings->value(section+"/address").toString();
129  } else if (settings->contains(m_section2+"/address")) {
130  m_address = settings->value(m_section2+"/address").toString();
131  // qDebug() << "obsahuje sekce 2, nastavuji" << settings->value(section+"/address").toString();
132  } else {
133  // qDebug() << "neobsahuje, nastavuji" << m_default_address;
134  m_address = m_default_address;
135  }
136  m_timeout = settings->value( section + "/timeout",
137  settings->value(m_section2 + "/timeout", m_default_timeout)).toInt();
138  m_maxAge = settings->value( section + "/maxAge",
139  settings->value(m_section2 + "/maxAge", m_default_maxAge)).toInt();
140  m_encoding = settings->value( section + "/encoding",
141  settings->value(m_section2 + "/encoding", m_default_encoding)).toString();
142  m_docroot = settings->value( section + "/root",
143  settings->value(m_section2 + "/root", m_default_docroot)).toString();
144  m_indexFile = settings->value( section + "/indexFile",
145  settings->value(m_section2 + "/indexFile", m_default_indexFile)).toString();
146  m_sessionCookieName = settings->value( section + "/sessionCookieName",
147  settings->value(m_section2 + "/sessionCookieName", m_default_sessionCookieName)).toString();
148  m_sessionExpirationTime = settings->value( section + "/sessionExpirationTime",
149  settings->value(m_section2 + "/sessionExpirationTime", m_default_sessionExpirationTime)).toInt();
150  m_maxRequestSize = settings->value( section + "/maxRequestSize",
151  settings->value(m_section2 + "/maxRequestSize", m_default_maxRequestSize)).toInt();
152  m_maxMultiPartSize = settings->value( section + "/maxMultiPartSize",
153  settings->value(m_section2 + "/maxMultiPartSize", m_default_maxMultiPartSize)).toInt();
154  m_useSSL = settings->value( section + "/useSSL",
155  settings->value(m_section2 + "/useSSL", m_default_useSSL)).toBool();
156  m_sslKey = settings->value( section + "/sslKey",
157  settings->value(m_section2 + "/sslKey", m_default_sslKey)).toString();
158  m_sslCrt = settings->value( section + "/sslCrt",
159  settings->value(m_section2 + "/sslCrt", m_default_sslCrt)).toString();
160  m_sslCaCrt = settings->value( section + "/sslCaCrt",
161  settings->value(m_section2 + "/sslCaCrt", m_default_sslCaCrt)).toString();
162  m_ignoreAllSslErrors = settings->value( section + "/IgnoreAllSslErrors",
163  settings->value(m_section2 + "/IgnoreAllSslErrors", m_default_ignoreAllSslErrors)).toBool();
164  m_threads = settings->value( section + "/threads",
165  settings->value(m_section2 + "/threads", m_default_threads)).toBool();
166  m_gzip = settings->value( section + "/gzip",
167  settings->value(m_section2 + "/gzip", m_default_gzip)).toBool();
168 
169  #define SSLERROR(x) { if (settings->value( section + "/Ignore" + #x, \
170  settings->value(m_section2 + "/Ignore" + #x, false)).toBool()) { \
171  m_sslErrors << QSslError::x; \
172  } \
173  }
174 
175  SSLERROR(UnableToGetIssuerCertificate);
176  SSLERROR(UnableToDecryptCertificateSignature);
177  SSLERROR(UnableToDecodeIssuerPublicKey);
178  SSLERROR(CertificateSignatureFailed);
179  SSLERROR(CertificateNotYetValid);
180  SSLERROR(CertificateExpired);
181  SSLERROR(InvalidNotBeforeField);
182  SSLERROR(InvalidNotAfterField);
183  SSLERROR(SelfSignedCertificate);
184  SSLERROR(SelfSignedCertificateInChain);
185  SSLERROR(UnableToGetLocalIssuerCertificate);
186  SSLERROR(UnableToVerifyFirstCertificate);
187  SSLERROR(CertificateRevoked);
188  SSLERROR(InvalidCaCertificate);
189  SSLERROR(PathLengthExceeded);
190  SSLERROR(InvalidPurpose);
191  SSLERROR(CertificateUntrusted);
192  SSLERROR(CertificateRejected);
193  SSLERROR(SubjectIssuerMismatch);
194  SSLERROR(AuthorityIssuerSerialNumberMismatch);
195  SSLERROR(NoPeerCertificate);
196  SSLERROR(HostNameMismatch);
197  SSLERROR(UnspecifiedError);
198  SSLERROR(NoSslSupport);
199  #if QT_VERSION > 0x040700
200  SSLERROR(CertificateBlacklisted);
201  #endif
202 
203 }
204 
205 
206 HttpSettings::HttpSettings(const QSettings *settings, const QString& section, QObject *parent) : QObject(parent) {
207  setDefaultValues();
208  QString m_section2 = "http";
209  readSettings(settings, section);
210 }
211 
212 
213 HttpSettings::HttpSettings(const QSettings *settings, QObject *parent) : QObject(parent) {
214  setDefaultValues();
215  QString m_section2 = "http";
216  readSettings(settings, "http");
217 }
218 
219 
220 bool HttpSettings::ignoreSslError(QSslError error) const {
221  if (m_ignoreAllSslErrors) {
222  return true;
223  }
224  return m_sslErrors.contains(error);
225 }
226 
227 
void readSettings(const QSettings *settings, const QString &section, const QString &section2)
Read settings from configuration file.
bool ignoreSslError(QSslError error) const
Returns true if the error should be ignored, default true.
HttpSettings(QObject *parent)
Constructor reads default QSettings file section [httpd].
Namespace of HTTP server.