Google APIs Client Library for C++
mongoose_webserver.h
Go to the documentation of this file.
00001 /*
00002  * \copyright Copyright 2013 Google Inc. All Rights Reserved.
00003  * \license @{
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  *
00017  * @}
00018  */
00019 
00020 
00021 #ifndef APISERVING_CLIENTS_CPP_UTIL_MONGOOSE_WEBSERVER_H_
00022 #define APISERVING_CLIENTS_CPP_UTIL_MONGOOSE_WEBSERVER_H_
00023 
00024 #include <map>
00025 using std::map;
00026 #include <vector>
00027 using std::vector;
00028 #include "googleapis/client/util/abstract_webserver.h"
00029 #include "googleapis/base/callback.h"
00030 #include "googleapis/base/macros.h"
00031 #include "googleapis/strings/stringpiece.h"
00032 #include "googleapis/util/status.h"
00033 #include <mongoose/mongoose.h>
00034 namespace googleapis {
00035 
00036 namespace client {
00037 
00038 /*
00039  * Provides a web server for samples and testing.
00040  * @ingroup PlatformLayerWebServer
00041  *
00042  * This class and the use of Mongoose Web Server available from
00043  * https://code.google.com/p/mongoose/
00044  *
00045  * This class is currently only intended to support testing and tinkering.
00046  * It is not robust for production use. The underlying library is probably
00047  * sufficient, but this wrapper uses only minimal configuration and processing.
00048  */
00049 class MongooseWebServer : public AbstractWebServer {
00050  public:
00051   static const StringPiece ACCESS_LOG_FILE;
00052   static const StringPiece DOCUMENT_ROOT;
00053   static const StringPiece ENABLE_KEEP_ALIVE;
00054   static const StringPiece ERROR_LOG_FILE;
00055   static const StringPiece LISTENING_PORTS;
00056   static const StringPiece NUM_THREADS;
00057   static const StringPiece REQUEST_TIMEOUT_MS;
00058   static const StringPiece SSL_CERTIFICATE;
00059 
00060   /*
00061    * Constructs an http server on the given port
00062    *
00063    * @param[in] port Should be non-0.
00064    */
00065   explicit MongooseWebServer(int port);
00066 
00067   /*
00068    * Standard destructor.
00069    */
00070   virtual ~MongooseWebServer();
00071 
00072   /*
00073    * Determines whether we are using SSL or not.
00074    * @return if we'll use SSL (https).
00075    */
00076   bool use_ssl() const {
00077     return !mongoose_option(kSslCertificateOption).empty();
00078   }
00079 
00080   /*
00081    * Override Mongoose options.
00082    *
00083    * This replaces the old options that were overriden.
00084    *
00085    * @param[in] options The options will be copied. They must be set
00086    *            before the server is started.
00087    */
00088   void set_mongoose_options(const map<string, string>& options) {
00089     options_ = options;
00090   }
00091 
00092   /*
00093    * Returns Mongoose options that were overriden.
00094    */
00095   const map<string, string>& mongoose_options() const { return options_; }
00096 
00097   /*
00098    * Clears Mongoose option overrides.
00099    */
00100   void clear_mongoose_options() { options_.clear(); }
00101 
00102   /*
00103    * Explicitly configure an individual Mongoose server option.
00104    *
00105    * @param[in] name See the Mongoose Documentation for option names.
00106    * @param[in] value See the Mongoose Documentation for option values.
00107    */
00108   void set_mongoose_option(
00109       const StringPiece& name, const StringPiece& value) {
00110     options_.insert(make_pair(name.as_string(), value.as_string()));
00111   }
00112 
00113   /*
00114    * Returns value for individual option, or empty if not set.
00115    */
00116   const string mongoose_option(const string& name) const {
00117     map<string, string>::const_iterator it = options_.find(name);
00118     if (it == options_.end()) return "";
00119     return it->second;
00120   }
00121 
00122   /*
00123    * Clears an overiden Mongoose options back to the default value.
00124    */
00125   void clear_mongoose_option(const StringPiece& name) {
00126     map<string, string>::iterator it = options_.find(name.as_string());
00127     if (it != options_.end()) {
00128       options_.erase(it);
00129     }
00130   }
00131 
00132   /*
00133    * Returns actual protocol depending on whether SSL was enabled.
00134    */
00135   virtual string url_protocol() const;
00136 
00137  protected:
00138   /*
00139    * Starts the server.
00140    *
00141    * @return ok or reason for error.
00142    */
00143   virtual util::Status DoStartup();
00144 
00145   /*
00146    * Stops the server.
00147    *
00148    * @return ok or reason for error.
00149    */
00150   virtual void DoShutdown();
00151 
00152  protected:
00153   /*
00154    * Sends the body response with the given http_code.
00155    *
00156    * @param[in] content_type The MIME content_type for the response.
00157    * @param[in] http_code The HTTP status code to return.
00158    * @param[in] body The HTTP body to return.
00159    * @param[in] connection The connection passed to the DoHandleUrl.
00160    */
00161   int SendResponse(
00162       const StringPiece& content_type,
00163       int http_code,
00164       const StringPiece& body,
00165       struct mg_connection* connection);
00166 
00167  private:
00168   const string kSslCertificateOption;
00169   std::map<string, string> options_;
00170   struct mg_callbacks callbacks_;
00171   struct mg_context* mg_context_;
00172 
00173   static int BeginRequestHandler(struct mg_connection* conn);
00174 
00175   DISALLOW_COPY_AND_ASSIGN(MongooseWebServer);
00176 };
00177 
00178 }  // namespace client
00179 
00180 } // namespace googleapis
00181 #endif  // APISERVING_CLIENTS_CPP_UTIL_MONGOOSE_WEBSERVER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines