Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
bel2125 committed Dec 27, 2018
2 parents 410c00e + 09de6cd commit e440e06
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 41 deletions.
2 changes: 1 addition & 1 deletion docs/APIReference.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The content of both structures is not defined in the interface - they are only u
* [`struct mg_option;`](api/mg_option.md)
* [`struct mg_request_info;`](api/mg_request_info.md)
* [`struct mg_response_info;`](api/mg_response_info.md)
* [`struct mg_server_ports;`](api/mg_server_ports.md)
* [`struct mg_server_port;`](api/mg_server_port.md)


## Library API Functions
Expand Down
2 changes: 1 addition & 1 deletion docs/api/mg_get_ports.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ The function returns the number of ports actually stored in the buffer.

### See Also

* [`struct mg_server_ports;`](mg_server_ports.md)
* [`struct mg_server_port;`](mg_server_port.md)
* [`mg_get_server_ports();`](mg_get_server_ports.md)
4 changes: 2 additions & 2 deletions docs/api/mg_get_server_ports.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
| :--- | :--- | :--- |
|**`ctx`**|`const struct mg_context *`|The context for which the server ports are requested|
|**`size`**|`int`|The size of the buffer to store the port information|
|**`ports`**|`struct mg_server_ports *`|Buffer to store the port information|
|**`ports`**|`struct mg_server_port *`|Buffer to store the port information|

### Return Value

Expand All @@ -25,4 +25,4 @@ The function returns the number of items in the list, or a negative value if an
### See Also

* [~~`mg_get_ports();`~~](mg_get_ports.md)
* [`struct mg_server_ports;`](mg_server_ports.md)
* [`struct mg_server_port;`](mg_server_port.md)
4 changes: 2 additions & 2 deletions docs/api/mg_server_ports.md → docs/api/mg_server_port.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Civetweb API Reference

### `struct mg_server_ports;`
### `struct mg_server_port;`

### Fields

Expand All @@ -17,7 +17,7 @@

### Description

A call to the function [`mg_get_server_ports()`](mg_get_server_ports.md) returns a list of structures with information about each running Civetweb service. These structures are of type `struct mg_server_ports` and contain the base information of each service.
A call to the function [`mg_get_server_ports()`](mg_get_server_ports.md) returns a list of structures with information about each running Civetweb service. These structures are of type `struct mg_server_port` and contain the base information of each service.

### See Also

Expand Down
2 changes: 1 addition & 1 deletion examples/embedded_c/embedded_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ main(int argc, char *argv[])
0};
struct mg_callbacks callbacks;
struct mg_context *ctx;
struct mg_server_ports ports[32];
struct mg_server_port ports[32];
int port_cnt, n;
int err = 0;

Expand Down
2 changes: 1 addition & 1 deletion include/CivetServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ class CIVETWEB_CXX_API CivetServer
*
* @return A vector of ports
*/
std::vector<struct mg_server_ports> getListeningPortsFull();
std::vector<struct mg_server_port> getListeningPortsFull();

/**
* getCookie(struct mg_connection *conn, const std::string &cookieName,
Expand Down
9 changes: 6 additions & 3 deletions include/civetweb.h
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ enum {
CIVETWEB_API const struct mg_option *mg_get_valid_options(void);


struct mg_server_ports {
struct mg_server_port {
int protocol; /* 1 = IPv4, 2 = IPv6, 3 = both */
int port; /* port number */
int is_ssl; /* https port: 0 = no, 1 = yes */
Expand All @@ -692,15 +692,18 @@ struct mg_server_ports {
int _reserved4;
};

/* Legacy name */
#define mg_server_ports mg_server_port


/* Get the list of ports that civetweb is listening on.
The parameter size is the size of the ports array in elements.
The caller is responsibility to allocate the required memory.
This function returns the number of struct mg_server_ports elements
This function returns the number of struct mg_server_port elements
filled in, or <0 in case of an error. */
CIVETWEB_API int mg_get_server_ports(const struct mg_context *ctx,
int size,
struct mg_server_ports *ports);
struct mg_server_port *ports);


#if defined(MG_LEGACY_INTERFACE) /* 2017-04-02 */
Expand Down
6 changes: 3 additions & 3 deletions src/CivetServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ CivetServer::urlEncode(const char *src,
std::vector<int>
CivetServer::getListeningPorts()
{
std::vector<struct mg_server_ports> server_ports = getListeningPortsFull();
std::vector<struct mg_server_port> server_ports = getListeningPortsFull();

std::vector<int> ports(server_ports.size());
for (size_t i = 0; i < server_ports.size(); i++) {
Expand All @@ -633,10 +633,10 @@ CivetServer::getListeningPorts()
return ports;
}

std::vector<struct mg_server_ports>
std::vector<struct mg_server_port>
CivetServer::getListeningPortsFull()
{
std::vector<struct mg_server_ports> server_ports(50);
std::vector<struct mg_server_port> server_ports(50);
int size = mg_get_server_ports(context,
(int)server_ports.size(),
&server_ports[0]);
Expand Down
49 changes: 25 additions & 24 deletions src/civetweb.c
Original file line number Diff line number Diff line change
Expand Up @@ -2753,7 +2753,7 @@ struct mg_connection {
* throttle */

time_t last_throttle_time; /* Last time throttled data was sent */
int64_t last_throttle_bytes; /* Bytes sent this second */
int last_throttle_bytes; /* Bytes sent this second */
pthread_mutex_t mutex; /* Used by mg_(un)lock_connection to ensure
* atomic transmissions for websockets */
#if defined(USE_LUA) && defined(USE_WEBSOCKET)
Expand Down Expand Up @@ -3504,7 +3504,7 @@ mg_get_ports(const struct mg_context *ctx, size_t size, int *ports, int *ssl)
int
mg_get_server_ports(const struct mg_context *ctx,
int size,
struct mg_server_ports *ports)
struct mg_server_port *ports)
{
int i, cnt = 0;

Expand Down Expand Up @@ -6255,16 +6255,16 @@ push_inner(struct mg_context *ctx,
}


static int64_t
static int
push_all(struct mg_context *ctx,
FILE *fp,
SOCKET sock,
SSL *ssl,
const char *buf,
int64_t len)
int len)
{
double timeout = -1.0;
int64_t n, nwritten = 0;
int n, nwritten = 0;

if (ctx == NULL) {
return -1;
Expand All @@ -6275,10 +6275,10 @@ push_all(struct mg_context *ctx,
}

while ((len > 0) && (ctx->stop_flag == 0)) {
n = push_inner(ctx, fp, sock, ssl, buf + nwritten, (int)len, timeout);
n = push_inner(ctx, fp, sock, ssl, buf + nwritten, len, timeout);
if (n < 0) {
if (nwritten == 0) {
nwritten = n; /* Propagate the error */
nwritten = -1; /* Propagate the error */
}
break;
} else if (n == 0) {
Expand Down Expand Up @@ -6760,40 +6760,43 @@ int
mg_write(struct mg_connection *conn, const void *buf, size_t len)
{
time_t now;
int64_t n, total, allowed;
int n, total, allowed;

if (conn == NULL) {
return 0;
}
if (len > INT_MAX) {
return -1;
}

if (conn->throttle > 0) {
if ((now = time(NULL)) != conn->last_throttle_time) {
conn->last_throttle_time = now;
conn->last_throttle_bytes = 0;
}
allowed = conn->throttle - conn->last_throttle_bytes;
if (allowed > (int64_t)len) {
allowed = (int64_t)len;
if (allowed > (int)len) {
allowed = (int)len;
}
if ((total = push_all(conn->phys_ctx,
NULL,
conn->client.sock,
conn->ssl,
(const char *)buf,
(int64_t)allowed))
allowed))
== allowed) {
buf = (const char *)buf + total;
conn->last_throttle_bytes += total;
while ((total < (int64_t)len) && (conn->phys_ctx->stop_flag == 0)) {
allowed = (conn->throttle > ((int64_t)len - total))
? (int64_t)len - total
while ((total < (int)len) && (conn->phys_ctx->stop_flag == 0)) {
allowed = (conn->throttle > ((int)len - total))
? (int)len - total
: conn->throttle;
if ((n = push_all(conn->phys_ctx,
NULL,
conn->client.sock,
conn->ssl,
(const char *)buf,
(int64_t)allowed))
allowed))
!= allowed) {
break;
}
Expand All @@ -6810,12 +6813,12 @@ mg_write(struct mg_connection *conn, const void *buf, size_t len)
conn->client.sock,
conn->ssl,
(const char *)buf,
(int64_t)len);
(int)len);
}
if (total > 0) {
conn->num_bytes_sent += total;
}
return (int)total;
return total;
}


Expand Down Expand Up @@ -10529,7 +10532,7 @@ forward_body_data(struct mg_connection *conn, FILE *fp, SOCKET sock, SSL *ssl)
const char *expect, *body;
char buf[MG_BUF_LEN];
int to_read, nread, success = 0;
int64_t buffered_len;
int buffered_len;
double timeout = -1.0;

if (!conn) {
Expand Down Expand Up @@ -10568,8 +10571,7 @@ forward_body_data(struct mg_connection *conn, FILE *fp, SOCKET sock, SSL *ssl)
conn->status_code = 200;
}

buffered_len = (int64_t)(conn->data_len) - (int64_t)conn->request_len
- conn->consumed_content;
buffered_len = conn->data_len - conn->request_len;

DEBUG_ASSERT(buffered_len >= 0);
DEBUG_ASSERT(conn->consumed_content == 0);
Expand All @@ -10583,9 +10585,8 @@ forward_body_data(struct mg_connection *conn, FILE *fp, SOCKET sock, SSL *ssl)
if ((int64_t)buffered_len > conn->content_len) {
buffered_len = (int)conn->content_len;
}
body = conn->buf + conn->request_len + conn->consumed_content;
push_all(
conn->phys_ctx, fp, sock, ssl, body, (int64_t)buffered_len);
body = conn->buf + conn->request_len;
push_all(conn->phys_ctx, fp, sock, ssl, body, buffered_len);
conn->consumed_content += buffered_len;
}

Expand All @@ -10605,8 +10606,8 @@ forward_body_data(struct mg_connection *conn, FILE *fp, SOCKET sock, SSL *ssl)
!= nread) {
break;
}
conn->consumed_content += nread;
}
conn->consumed_content += nread;
}

if (conn->consumed_content == conn->content_len) {
Expand Down
6 changes: 3 additions & 3 deletions unittest/public_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ test_mg_start_stop_http_server_impl(int ipv6, int bound)
char client_err[256];
const struct mg_response_info *client_ri;
int client_res, ret;
struct mg_server_ports portinfo[8];
struct mg_server_port portinfo[8];

mark_point();

Expand Down Expand Up @@ -585,7 +585,7 @@ START_TEST(test_mg_start_stop_https_server)
char client_err[256];
const struct mg_response_info *client_ri;
int client_res, ret;
struct mg_server_ports portinfo[8];
struct mg_server_port portinfo[8];

ck_assert(ssl_cert != NULL);

Expand Down Expand Up @@ -699,7 +699,7 @@ START_TEST(test_mg_server_and_client_tls)
struct mg_context *ctx;

int ports_cnt;
struct mg_server_ports ports[16];
struct mg_server_port ports[16];
struct mg_callbacks callbacks;
char errmsg[256];

Expand Down

0 comments on commit e440e06

Please sign in to comment.