Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uStaticServer : First Full-Featured Gui based example #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions commondir.pri
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# specifying common dirs

# comment following line to build the lib as static library
DEFINES *= QHTTP_DYNAMIC_LIB
# comment following line to trim client classes from build
DEFINES *= QHTTP_HAS_CLIENT
# Qt5.5.1 on OSX needs both c++11 and c++14!! the c++14 is not enough
CONFIG += c++11 c++14

unix {
TEMPDIR = $$PRJDIR/tmp/unix/$$TARGET
macx:TEMPDIR = $$PRJDIR/tmp/osx/$$TARGET
TEMPDIR = $$PRJDIR/tmp/unix/$$TARGET
macx:TEMPDIR = $$PRJDIR/tmp/osx/$$TARGET
}

win32 {
warning("Windows port of this library has not been tested nor profiled.")
TEMPDIR = $$PRJDIR/tmp/win32/$$TARGET
DEFINES += _WINDOWS WIN32_LEAN_AND_MEAN NOMINMAX
TEMPDIR = $$PRJDIR/tmp/win32/$$TARGET
DEFINES += _WINDOWS WIN32_LEAN_AND_MEAN NOMINMAX
}


8 changes: 6 additions & 2 deletions example/example.pro
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
TEMPLATE = subdirs

SUBDIRS += helloworld
SUBDIRS += helloworld \
uStaticServer
SUBDIRS += postcollector
SUBDIRS += basic-server
SUBDIRS += keep-alive

contains(DEFINES, QHTTP_HAS_CLIENT) {
SUBDIRS += keep-alive
}

11 changes: 11 additions & 0 deletions example/uStaticServer/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}
135 changes: 135 additions & 0 deletions example/uStaticServer/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"


#include "qhttpserver.hpp"
#include "qhttpserverconnection.hpp"
#include "qhttpserverrequest.hpp"
#include "qhttpserverresponse.hpp"

#include <QtCore/QCoreApplication>
#include <QDateTime>
#include <QLocale>

#include "../include/unixcatcher.hpp"

#include <QFile>
#include <QFileDialog>

#include <map>
#include <fstream>

using namespace qhttp::server;
using namespace std;

#define Y2X(X) #X
#define MAC2STR(Y) Y2X(Y)
#define SITEROOT .


map<string,string> mime;

QString root=".";
QFileDialog *getD;
map<QString, QHttpServer> servers;

void fileProvider(QHttpRequest* req, QHttpResponse* res)
{
QString docname=root+(req->url().toString()==("/") ?("/index.html"):req->url().toString()) ;
QFile doc(docname);

if(not doc.open(QFile::ReadOnly))
{
QByteArray body = "Sorry Not found";
res->addHeader("Content-Length", QString::number(body.size()).toUtf8());
res->setStatusCode(qhttp::TStatusCode::ESTATUS_NOT_FOUND);
res->write(body);
return;
}

try {
auto nm=docname.toStdString();
auto ext=nm.substr(nm.find_last_of('.'));
res->addHeader("Content-Type",mime[ext].data());
} catch (...) {
res->addHeader("Content-Type","application/octet-stream");
}

res->addHeader("Content-Length", QString::number(doc.size()).toUtf8());
res->addHeader("Access-Control-Allow-Origin"," * ");
res->addHeader("Access-Control-Allow-Headers","GET,POST,PUT");
res->setStatusCode(qhttp::TStatusCode::ESTATUS_OK);
res->write(doc.readAll());
}



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

getD=new QFileDialog();
getD->setFileMode(QFileDialog::Directory);
getD->setOption(QFileDialog::ShowDirsOnly);

ui->rootE->setText(root);

if(mime.empty())
{
QFile fk(":/mime_types.txt");
fk.open(QIODevice::ReadOnly | QIODevice::Text);

do
{
QString mime_kv_pair;
mime_kv_pair=fk.readLine();
auto kv=mime_kv_pair.split("\t");
mime[kv[0].toStdString()]=kv[1].toStdString();
}while(fk.canReadLine());
}

this->on_addPortBt_clicked();
}

MainWindow::~MainWindow()
{
delete ui;
}




void MainWindow::on_chRtBt_clicked()
{
if(getD->exec())
ui->rootE->setText(root=getD->selectedFiles()[0]);
}

void MainWindow::on_addPortBt_clicked()
{
auto np=ui->portE->text();
if(servers.find(np)==servers.end())
{
servers[np].listen(np,fileProvider);
if(servers[np].isListening())
ui->portsE->addItem(np);
else
servers.erase(np);
}
}

void MainWindow::on_rmPortBt_clicked()
{
if(ui->portsE->count()>0)
{
servers.erase(ui->portsE->currentItem()->text());
ui->portsE->takeItem(ui->portsE->currentRow());
}
}

void MainWindow::on_rootE_textChanged(const QString &nroot)
{
root=nroot;
}
31 changes: 31 additions & 0 deletions example/uStaticServer/mainwindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_chRtBt_clicked();

void on_addPortBt_clicked();

void on_rmPortBt_clicked();

void on_rootE_textChanged(const QString &arg1);

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
92 changes: 92 additions & 0 deletions example/uStaticServer/mainwindow.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>174</width>
<height>313</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLineEdit" name="rootE"/>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="chRtBt">
<property name="text">
<string>ChangeRoot</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="rmPortBt">
<property name="text">
<string>Remove Port</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="addPortBt">
<property name="text">
<string>Add Port</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QSpinBox" name="portE">
<property name="maximum">
<number>65535</number>
</property>
<property name="value">
<number>80</number>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QListWidget" name="portsE"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>174</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionExit"/>
</widget>
<addaction name="menuFile"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionExit">
<property name="text">
<string>Exit</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
5 changes: 5 additions & 0 deletions example/uStaticServer/mime_list_1.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource>
<file>mime_types.txt</file>
</qresource>
</RCC>
646 changes: 646 additions & 0 deletions example/uStaticServer/mime_types.txt

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions example/uStaticServer/uStaticServer.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#-------------------------------------------------
#
# Project created by QtCreator 2017-01-20T01:28:47
#
#-------------------------------------------------

QT += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = uStaticServer
TEMPLATE = app


PRJDIR = ../..
include($$PRJDIR/commondir.pri)

SOURCES += main.cpp\
mainwindow.cpp

LIBS += -lqhttp

HEADERS += mainwindow.h

FORMS += mainwindow.ui

RESOURCES += \
mime_list_1.qrc