Skip to content
Snippets Groups Projects
mongoose.c 113 KiB
Newer Older
Sergey Lyubka's avatar
Sergey Lyubka committed
// Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
// Copyright (c) 2013-2014 Cesanta Software Limited
Sergey Lyubka's avatar
Sergey Lyubka committed
// All rights reserved
//
// This library is dual-licensed: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation. For the terms of this
// license, see <http://www.gnu.org/licenses/>.
//
// You are free to use this library under the terms of the GNU General
// Public License, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// Alternatively, you can license this library under a commercial
// license, as set out in <http://cesanta.com/>.
Sergey Lyubka's avatar
Sergey Lyubka committed
// net_skeleton start
#include "net_skeleton.h"
// net_skeleton end
Sergey Lyubka's avatar
Sergey Lyubka committed

#include <ctype.h>

#ifdef _WIN32
#define S_ISDIR(x) ((x) & _S_IFDIR)
#define sleep(x) Sleep((x) * 1000)
#define stat(x, y) mg_stat((x), (y))
#define fopen(x, y) mg_fopen((x), (y))
#define open(x, y) mg_open((x), (y))
#define lseek(x, y, z) _lseeki64((x), (y), (z))
#define mkdir(x, y) _mkdir(x)
#define to64(x) _atoi64(x)
#ifndef __func__
#define STRX(x) #x
#define STR(x) STRX(x)
#define __func__ __FILE__ ":" STR(__LINE__)
Sergey Lyubka's avatar
Sergey Lyubka committed
#endif
Sergey Lyubka's avatar
Sergey Lyubka committed
typedef struct _stati64 file_stat_t;
Sergey Lyubka's avatar
Sergey Lyubka committed
#include <dirent.h>
Sergey Lyubka's avatar
Sergey Lyubka committed
#include <inttypes.h>
#include <pwd.h>
#define O_BINARY 0
Sergey Lyubka's avatar
Sergey Lyubka committed
#define INT64_FMT PRId64
typedef struct stat file_stat_t;
Sergey Lyubka's avatar
Sergey Lyubka committed
#endif
Sergey Lyubka's avatar
Sergey Lyubka committed
#include "mongoose.h"

#define MAX_REQUEST_SIZE 16384
#define IOBUF_SIZE 8192
#define MAX_PATH_SIZE 8192
Sergey Lyubka's avatar
Sergey Lyubka committed
#define LUA_SCRIPT_PATTERN "**.lp$"
#define DEFAULT_CGI_PATTERN "**.cgi$|**.pl$|**.php$"
#define CGI_ENVIRONMENT_SIZE 8192
Sergey Lyubka's avatar
Sergey Lyubka committed
#define MAX_CGI_ENVIR_VARS 64
#define ENV_EXPORT_TO_CGI "MONGOOSE_CGI"
#define PASSWORDS_FILE_NAME ".htpasswd"
#ifndef MONGOOSE_USE_WEBSOCKET_PING_INTERVAL
#define MONGOOSE_USE_WEBSOCKET_PING_INTERVAL 5
Sergey Lyubka's avatar
Sergey Lyubka committed

// Extra HTTP headers to send in every static file reply
#if !defined(MONGOOSE_USE_EXTRA_HTTP_HEADERS)
#define MONGOOSE_USE_EXTRA_HTTP_HEADERS ""
Sergey Lyubka's avatar
Sergey Lyubka committed
#ifndef MONGOOSE_POST_SIZE_LIMIT
#define MONGOOSE_POST_SIZE_LIMIT 0
Sergey Lyubka's avatar
Sergey Lyubka committed
#endif

Sergey Lyubka's avatar
Sergey Lyubka committed
#ifndef MONGOOSE_IDLE_TIMEOUT_SECONDS
#define MONGOOSE_IDLE_TIMEOUT_SECONDS 30
Sergey Lyubka's avatar
Sergey Lyubka committed
#ifdef MONGOOSE_NO_SOCKETPAIR
#define MONGOOSE_NO_CGI
#endif

#ifdef MONGOOSE_NO_FILESYSTEM
#define MONGOOSE_NO_AUTH
#define MONGOOSE_NO_CGI
#define MONGOOSE_NO_DAV
#define MONGOOSE_NO_DIRECTORY_LISTING
#define MONGOOSE_NO_LOGGING
Sergey Lyubka's avatar
Sergey Lyubka committed
#endif

Sergey Lyubka's avatar
Sergey Lyubka committed
struct vec {
  const char *ptr;
// For directory listing and WevDAV support
struct dir_entry {
  struct connection *conn;
  char *file_name;
  file_stat_t st;
Sergey Lyubka's avatar
Sergey Lyubka committed
};

// NOTE(lsm): this enum shoulds be in sync with the config_options.
enum {
Sergey Lyubka's avatar
Sergey Lyubka committed
  ACCESS_CONTROL_LIST,
#ifndef MONGOOSE_NO_FILESYSTEM
  ACCESS_LOG_FILE,
#ifndef MONGOOSE_NO_AUTH
  AUTH_DOMAIN,
#endif
#ifndef MONGOOSE_NO_CGI
  CGI_INTERPRETER,
  CGI_PATTERN,
#endif
#ifndef MONGOOSE_NO_DAV
  DAV_AUTH_FILE,
#endif
  DOCUMENT_ROOT,
#ifndef MONGOOSE_NO_DIRECTORY_LISTING
  ENABLE_DIRECTORY_LISTING,
#endif
Sergey Lyubka's avatar
Sergey Lyubka committed
#endif
  EXTRA_MIME_TYPES,
#if !defined(MONGOOSE_NO_FILESYSTEM) && !defined(MONGOOSE_NO_AUTH)
Sergey Lyubka's avatar
Sergey Lyubka committed
  GLOBAL_AUTH_FILE,
#endif
  HIDE_FILES_PATTERN,
#ifndef MONGOOSE_NO_FILESYSTEM
Sergey Lyubka's avatar
Sergey Lyubka committed
  INDEX_FILES,
#endif
  LISTENING_PORT,
#ifndef _WIN32
  RUN_AS_USER,
#endif
Sergey Lyubka's avatar
Sergey Lyubka committed
#ifdef NS_ENABLE_SSL
  URL_REWRITES,
  NUM_OPTIONS
Sergey Lyubka's avatar
Sergey Lyubka committed
static const char *static_config_options[] = {
  "access_control_list", NULL,
#ifndef MONGOOSE_NO_FILESYSTEM
Sergey Lyubka's avatar
Sergey Lyubka committed
  "access_log_file", NULL,
#ifndef MONGOOSE_NO_AUTH
Sergey Lyubka's avatar
Sergey Lyubka committed
  "auth_domain", "mydomain.com",
#endif
#ifndef MONGOOSE_NO_CGI
Sergey Lyubka's avatar
Sergey Lyubka committed
  "cgi_interpreter", NULL,
  "cgi_pattern", DEFAULT_CGI_PATTERN,
#endif
#ifndef MONGOOSE_NO_DAV
Sergey Lyubka's avatar
Sergey Lyubka committed
  "dav_auth_file", NULL,
Sergey Lyubka's avatar
Sergey Lyubka committed
  "document_root",  NULL,
#ifndef MONGOOSE_NO_DIRECTORY_LISTING
Sergey Lyubka's avatar
Sergey Lyubka committed
  "enable_directory_listing", "yes",
Sergey Lyubka's avatar
Sergey Lyubka committed
#endif
  "extra_mime_types", NULL,
#if !defined(MONGOOSE_NO_FILESYSTEM) && !defined(MONGOOSE_NO_AUTH)
Sergey Lyubka's avatar
Sergey Lyubka committed
  "global_auth_file", NULL,
#endif
  "hide_files_patterns", NULL,
#ifndef MONGOOSE_NO_FILESYSTEM
Sergey Lyubka's avatar
Sergey Lyubka committed
  "index_files","index.html,index.htm,index.cgi,index.php,index.lp",
#endif
  "listening_port", NULL,
#ifndef _WIN32
  "run_as_user", NULL,
#endif
Sergey Lyubka's avatar
Sergey Lyubka committed
#ifdef NS_ENABLE_SSL
Sergey Lyubka's avatar
Sergey Lyubka committed
  "ssl_certificate", NULL,
#endif
  "url_rewrites", NULL,
  NULL
};

struct mg_server {
Sergey Lyubka's avatar
Sergey Lyubka committed
  struct ns_server ns_server;
  union socket_address lsa;   // Listening socket address
  mg_handler_t request_handler;
  mg_handler_t error_handler;
  mg_handler_t auth_handler;
  char *config_options[NUM_OPTIONS];
// Local endpoint representation
union endpoint {
Sergey Lyubka's avatar
Sergey Lyubka committed
  int fd;                           // Opened regular local file
  struct ns_connection *cgi_conn;   // CGI socket
enum endpoint_type { EP_NONE, EP_FILE, EP_CGI, EP_USER, EP_PUT, EP_CLIENT };
Sergey Lyubka's avatar
Sergey Lyubka committed

#define MG_HEADERS_SENT NSF_USER_1
#define MG_LONG_RUNNING NSF_USER_2
Loading
Loading full blame...