Mongoose6.0源码分析(3)-重要结构体

原创 2015年11月19日 17:02:24

重要的结构体,但是不往下看很难明白要这么复杂的结构体,所以在读其他代码是慢慢分析

struct mg_mgr {

  struct mg_connection *active_connections;
  const char *hexdump_file; /* Debug hexdump file path */
  sock_t ctl[2];            /* Socketpair for mg_wakeup() */
  void *user_data;          /* User data */
  void *mgr_data;           /* Implementation-specific event manager's data. */ //实施特别事务管理员数据
};


struct mg_connection { //将老版本的mg_context和mg_connection结合了一下
  struct mg_connection *next, *prev; /* mg_mgr::active_connections linkage */ //搞链表
  struct mg_connection *listener;    /* Set only for accept()-ed connections */
  struct mg_mgr *mgr;                /* Pointer to containing manager */


  sock_t sock;             /* Socket to the remote peer */
  union socket_address sa; /* Remote peer address */
  size_t recv_mbuf_limit;  /* Max size of recv buffer */
  struct mbuf recv_mbuf;   /* Received data */ 接收到的数据存储在这个缓冲区中
  struct mbuf send_mbuf;   /* Data scheduled for sending */
  SSL *ssl;
  SSL_CTX *ssl_ctx;
  time_t last_io_time;              /* Timestamp of the last socket IO */
  mg_event_handler_t proto_handler; /* Protocol-specific event handler */
  void *proto_data;                 /* Protocol-specific data */
  mg_event_handler_t handler;       /* Event handler function */
  void *user_data;                  /* User-specific data */
  void *priv_1;                     /* Used by mg_enable_multithreading() */
  void *priv_2;                     /* Used by mg_enable_multithreading() */
  void *mgr_data; /* Implementation-specific event manager's data. */


  unsigned long flags;
/* Flags set by Mongoose */
#define MG_F_LISTENING (1 << 0)          /* This connection is listening */
#define MG_F_UDP (1 << 1)                /* This connection is UDP */
#define MG_F_RESOLVING (1 << 2)          /* Waiting for async resolver */
#define MG_F_CONNECTING (1 << 3)         /* connect() call in progress */
#define MG_F_SSL_HANDSHAKE_DONE (1 << 4) /* SSL specific */
#define MG_F_WANT_READ (1 << 5)          /* SSL specific */
#define MG_F_WANT_WRITE (1 << 6)         /* SSL specific */
#define MG_F_IS_WEBSOCKET (1 << 7)       /* Websocket specific */


/* Flags that are settable by user */
#define MG_F_SEND_AND_CLOSE (1 << 10)      /* Push remaining data and close  */
#define MG_F_DONT_SEND (1 << 11)           /* Do not send data to peer */
#define MG_F_CLOSE_IMMEDIATELY (1 << 12)   /* Disconnect */
#define MG_F_WEBSOCKET_NO_DEFRAG (1 << 13) /* Websocket specific */
#define MG_F_DELETE_CHUNK (1 << 14)        /* HTTP specific */


#define MG_F_USER_1 (1 << 20) /* Flags left for application */
#define MG_F_USER_2 (1 << 21)
#define MG_F_USER_3 (1 << 22)
#define MG_F_USER_4 (1 << 23)
#define MG_F_USER_5 (1 << 24)
#define MG_F_USER_6 (1 << 25)

};

struct mbuf {
  char *buf;   /* Buffer pointer */
  size_t len;  /* Data length. Data is located between offset 0 and len. */
  size_t size; /* Buffer size allocated by realloc(1). Must be >= len */
};

void mbuf_init(struct mbuf *mbuf, size_t initial_size) {
  mbuf->len = mbuf->size = 0;
  mbuf->buf = NULL;
  mbuf_resize(mbuf, initial_size);
}


struct http_message { //存放http消息,对照(2)看较清楚。
  struct
mg_str message; /* Whole message: request line + headers + body */
  struct mg_str proto; /* "HTTP/1.1" -- for both request and response */
  /* HTTP Request line (or HTTP response line) */
  struct mg_str method; /* "GET" */
  struct mg_str uri;    /* "/my_file.html" */
  /* For responses, code and response status message are set */
  int resp_code;
  struct mg_str resp_status_msg;
  /*
   * Query-string part of the URI. For example, for HTTP request
   *    GET /foo/bar?param1=val1&param2=val2
   *    |    uri    |     query_string     |
   *
   * Note that question mark character doesn't belong neither to the uri,
   * nor to the query_string
   */
  struct mg_str query_string;

  /* Headers */
  struct mg_str header_names[MG_MAX_HTTP_HEADERS]; //#define MG_MAX_HTTP_HEADERS 40
  struct mg_str header_values[MG_MAX_HTTP_HEADERS];

  /* Message body */
  struct mg_str body; /* Zero-length for requests with no body */
};


mg_str 

/* Describes chunk of memory */
struct mg_str {
  const char *p; /* Memory chunk pointer */
  size_t len;    /* Memory chunk length */
};


保存了要发送的数据体

struct proto_data_http {
  FILE *fp;         /* Opened file. */
  int64_t cl;       /* Content-Length. How many bytes to send. */
  int64_t sent;     /* How many bytes have been already sent. */
  int64_t body_len; /* How many bytes of chunked body was reassembled重组. */
  struct mg_connection *cgi_nc;
  enum http_proto_data_type type;
};


保存服务选项

struct mg_serve_http_opts {  
  /* Path to web root directory */
  const char *document_root;


  /* List of index files. Default is "" */
  const char *index_files;


  /*
   * Leave as NULL to disable authentication.
   * To enable directory protection with authentication, set this to ".htpasswd"
   * Then, creating ".htpasswd" file in any directory automatically protects
   * it with digest authentication.
   * Use `mongoose` web server binary, or `htdigest` Apache utility to
   * create/manipulate passwords file.
   * Make sure `auth_domain` is set to a valid domain name.
   */
  const char *per_directory_auth_file;


  /* Authorization domain (domain name of this web server) */
  const char *auth_domain;


  /*
   * Leave as NULL to disable authentication.
   * Normally, only selected directories in the document root are protected.
   * If absolutely every access to the web server needs to be authenticated,
   * regardless of the URI, set this option to the path to the passwords file.
   * Format of that file is the same as ".htpasswd" file. Make sure that file
   * is located outside document root to prevent people fetching it.
   */
  const char *global_auth_file;


  /* Set to "no" to disable directory listing. Enabled by default. */
  const char *enable_directory_listing;


  /* SSI files pattern. If not set, "**.shtml$|**.shtm$" is used. */
  const char *ssi_pattern;


  /* IP ACL. By default, NULL, meaning all IPs are allowed to connect */
  const char *ip_acl;


  /* URL rewrites.
   *
   * Comma-separated list of `uri_pattern=file_or_directory_path` rewrites.
   * When HTTP request is received, Mongoose constructs a file name from the
   * requested URI by combining `document_root` and the URI. However, if the
   * rewrite option is used and `uri_pattern` matches requested URI, then
   * `document_root` is ignored. Instead, `file_or_directory_path` is used,
   * which should be a full path name or a path relative to the web server's
   * current working directory. Note that `uri_pattern`, as all Mongoose
   * patterns, is a prefix pattern.
   *
   * If uri_pattern starts with `@` symbol, then Mongoose compares it with the
   * HOST header of the request. If they are equal, Mongoose sets document root
   * to `file_or_directory_path`, implementing virtual hosts support.
   */
  const char *url_rewrites;


  /* DAV document root. If NULL, DAV requests are going to fail. */
  const char *dav_document_root;


  /* Glob pattern for the files to hide. */
  const char *hidden_file_pattern;


  /* Set to non-NULL to enable CGI, e.g. **.cgi$|**.php$" */
  const char *cgi_file_pattern;


  /* If not NULL, ignore CGI script hashbang and use this interpreter */
  const char *cgi_interpreter;


  /*
   * Comma-separated list of Content-Type overrides for path suffixes, e.g.
   * ".txt=text/plain; charset=utf-8,.c=text/plain"
   */
  const char *custom_mime_types;
};

Mongoose源码分析

Mongoose是一个简易的web服务器,所谓web服务器,简单的说就是把服务断的数据返回给客户端。 的源码很简单,主要就是Mongoose.c文件,里面包含了大部分的处理。 Mongoose...
  • new_abc
  • new_abc
  • 2012年06月20日 15:22
  • 9122

Mongoose6.0源码分析(2)-http数据处理

从http_handle开始新的旅程
  • zxy355
  • zxy355
  • 2015年11月19日 16:58
  • 976

Mongoose6.0源码分析(1)-从一个example开始到读取数据

Mongoose6.0源码分析
  • zxy355
  • zxy355
  • 2015年10月22日 10:55
  • 2078

我读Mongoose源码----程序框架

分类: Unix网络编程2013-08-22 21:19 352人阅读 评论(4) 收藏 举报 Mongoose源代码线程Unix Mongoose是一种WEB服务器,因为最近在学习网...
  • pi9nc
  • pi9nc
  • 2014年04月07日 21:17
  • 2321

Mongoose源码分析

Mongoose是一个简易的web服务器,所谓web服务器,简单的说就是把服务断的数据返回给客户端。 的源码很简单,主要就是Mongoose.c文件,里面包含了大部分的处理。 Mong...
  • lifan_3a
  • lifan_3a
  • 2014年06月22日 19:13
  • 514

Mongoose6.0源码分析(3)-重要结构体

这方面应该先研究的,但是不往下看很难明白要这么复杂的结构体,所以在读其他代码是慢慢分析 struct mg_mgr {   struct mg_connection *active_connectio...
  • zxy355
  • zxy355
  • 2015年11月19日 17:02
  • 776

Mongoose源码分析之--简单的服务器搭建(C语言)

测试半天一直不行,发现原版少写个return null, 什么都不需要处理 可以实现web 由于在IOS终端设备上播放实时的TS流,要使用MPMoviePlayerController控件,必须采...
  • xp5xp6
  • xp5xp6
  • 2016年08月18日 16:56
  • 441

Nginx源码分析-4个重要结构之间的关系

Nginx源码之中有着很多很多的结构,当然每一个结构都是非常重要的,本文将分析其中的4个结构之间的关系。这4个结构分别是:listening、connection、event以及request。...
  • Marcky
  • Marcky
  • 2010年12月19日 15:43
  • 3213

【ffmpeg】常用结构体集合

看大牛雷霄骅的博客,再次精简出学习ffmpeg的一些知识, 本篇简要整理一下ffmpeg中常用的几个结构体,以作备忘。 本人所用ffmpeg源码版本:ffmpeg-2.8.3,注意,版本不同,某些...
  • u012819339
  • u012819339
  • 2015年12月16日 13:44
  • 2598

Mongoose源码分析之--简单的服务器搭建(C语言)

由于在IOS终端设备上播放实时的TS流,要使用MPMoviePlayerController控件,必须采用Http请求的形式去请求数据,而TS流的来源是通过HTTP请求拿到的,需要再把这个拿到的TS流...
  • u010351809
  • u010351809
  • 2015年03月02日 14:27
  • 1475
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Mongoose6.0源码分析(3)-重要结构体
举报原因:
原因补充:

(最多只允许输入30个字)