QxOrm  1.5.0
C++ Object Relational Mapping library
QxHttpServer.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** https://www.qxorm.com/
00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
00005 **
00006 ** This file is part of the QxOrm library
00007 **
00008 ** This software is provided 'as-is', without any express or implied
00009 ** warranty. In no event will the authors be held liable for any
00010 ** damages arising from the use of this software
00011 **
00012 ** Commercial Usage
00013 ** Licensees holding valid commercial QxOrm licenses may use this file in
00014 ** accordance with the commercial license agreement provided with the
00015 ** Software or, alternatively, in accordance with the terms contained in
00016 ** a written agreement between you and Lionel Marty
00017 **
00018 ** GNU General Public License Usage
00019 ** Alternatively, this file may be used under the terms of the GNU
00020 ** General Public License version 3.0 as published by the Free Software
00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the
00022 ** packaging of this file. Please review the following information to
00023 ** ensure the GNU General Public License version 3.0 requirements will be
00024 ** met : http://www.gnu.org/copyleft/gpl.html
00025 **
00026 ** If you are unsure which license is appropriate for your use, or
00027 ** if you have questions regarding the use of this file, please contact :
00028 ** contact@qxorm.com
00029 **
00030 ****************************************************************************/
00031 
00032 #ifdef Q_MOC_RUN
00033 #include <QxCommon/QxConfig.h> // Need to include this file for the 'moc' process
00034 #endif // Q_MOC_RUN
00035 
00036 #ifdef _QX_ENABLE_QT_NETWORK
00037 #ifndef _QX_HTTP_SERVER_H_
00038 #define _QX_HTTP_SERVER_H_
00039 
00040 #ifdef _MSC_VER
00041 #pragma once
00042 #endif
00043 
00051 #ifdef _QX_NO_PRECOMPILED_HEADER
00052 #ifndef Q_MOC_RUN
00053 #include <QxPrecompiled.h> // Need to include precompiled header for the generated moc file
00054 #endif // Q_MOC_RUN
00055 #endif // _QX_NO_PRECOMPILED_HEADER
00056 
00057 #include <QxHttpServer/QxHttpRequest.h>
00058 #include <QxHttpServer/QxHttpResponse.h>
00059 #include <QxHttpServer/QxHttpTransaction.h>
00060 
00061 #ifndef Q_MOC_RUN
00062 #include <QxService/QxTransaction.h>
00063 #include <QxService/QxConnect.h>
00064 #include <QxService/QxServer.h>
00065 #endif // Q_MOC_RUN
00066 
00067 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
00068 class QAbstractEventDispatcher;
00069 #endif // (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
00070 
00071 namespace qx {
00072 
00077 class QX_DLL_EXPORT QxHttpServer : public QObject
00078 {
00079 
00080    Q_OBJECT
00081 
00082 public:
00083 
00084    typedef std::function<void (qx::QxHttpRequest &, qx::QxHttpResponse &)> type_fct_custom_request_handler;
00085 
00086 private:
00087 
00088    struct QxHttpServerImpl;
00089    std::unique_ptr<QxHttpServerImpl> m_pImpl; 
00090 
00091 public:
00092 
00093    QxHttpServer(QObject * parent = NULL);
00094    virtual ~QxHttpServer();
00095 
00096    Q_INVOKABLE void startServer();
00097    Q_INVOKABLE void stopServer();
00098 
00099    void setCustomRequestHandler(type_fct_custom_request_handler fct);
00100    QxHttpServer & dispatch(const QString & command, const QString & path, type_fct_custom_request_handler fct, long position = -1);
00101    void beforeDispatching(type_fct_custom_request_handler fct);
00102    void afterDispatching(type_fct_custom_request_handler fct);
00103    void clearDispatcher();
00104 
00105 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
00106    void setEventDispatcher(QAbstractEventDispatcher * pEventDispatcher);
00107 #endif // (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
00108 
00109    static void buildResponseStaticFile(qx::QxHttpRequest & request, qx::QxHttpResponse & response, const QString & serverPath, qlonglong chunkedSize = 0);
00110    static void buildResponseQxRestApi(qx::QxHttpRequest & request, qx::QxHttpResponse & response);
00111 
00112 Q_SIGNALS:
00113 
00114    void error(const QString & err, qx::QxHttpTransaction_ptr transaction);
00115    void transactionStarted(qx::QxHttpTransaction_ptr transaction);
00116    void transactionFinished(qx::QxHttpTransaction_ptr transaction);
00117    void serverStatusChanged(bool bIsRunning);
00118 
00119 private Q_SLOTS:
00120 
00121    void onError(const QString & err, qx::service::QxTransaction_ptr transaction);
00122    void onServerIsRunning(bool bIsRunning, qx::service::QxServer * pServer);
00123    void onTransactionStarted(qx::service::QxTransaction_ptr transaction);
00124    void onTransactionFinished(qx::service::QxTransaction_ptr transaction);
00125    void onCustomRequestHandler(qx::service::QxTransaction_ptr transaction);
00126 
00127 };
00128 
00129 typedef std::shared_ptr<QxHttpServer> QxHttpServer_ptr;
00130 
00131 } // namespace qx
00132 
00133 typedef qx::QxHttpServer qx_http_server;
00134 
00135 #endif // _QX_HTTP_SERVER_H_
00136 #endif // _QX_ENABLE_QT_NETWORK