Skip to content
This repository was archived by the owner on Jul 14, 2019. It is now read-only.

Commit 8237578

Browse files
author
Lior Amram
committed
merge fixes: ssl, vmbuf, ringfile, sendmail
1 parent 3b14579 commit 8237578

21 files changed

+423
-269
lines changed

examples/httpd/src/httpd.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@ int main(int argc, char *argv[]) {
7777
{"port", 1, 0, 'p'},
7878
{"daemonize", 0, 0, 'd'},
7979
{"forks", 1, 0, 'f'},
80+
#ifdef RIBS2_SSL
8081
{"ssl_port", 1, 0 ,'s'},
8182
{"key_file", 1, 0, 'k'},
8283
{"chain_file", 1, 0, 'c'},
8384
{"cipher_list", 1, 0, 'l'},
85+
#endif
8486
{0, 0, 0, 0}
8587
};
8688

@@ -95,7 +97,11 @@ int main(int argc, char *argv[]) {
9597
#endif
9698
for (;;) {
9799
int option_index = 0;
98-
int c = getopt_long(argc, argv, "p:f:s:c:k:l:d", long_options, &option_index);
100+
int c = getopt_long(argc, argv, "dp:f:"
101+
#ifdef RIBS2_SSL
102+
"s:c:k:l:"
103+
#endif
104+
, long_options, &option_index);
99105
if (c == -1)
100106
break;
101107
switch (c) {
@@ -209,12 +215,28 @@ int main(int argc, char *argv[]) {
209215

210216
if (key_file && 0 > http_server_init2(&server_ssl))
211217
exit(EXIT_FAILURE);
218+
219+
if (sport == 0) {
220+
struct vmfile vmf = VMFILE_INITIALIZER;
221+
if (0 > vmfile_init(&vmf, "httpd.sport", 4096))
222+
exit(EXIT_FAILURE);
223+
vmfile_sprintf(&vmf, "%d", server_ssl.port);
224+
vmfile_close(&vmf);
225+
}
212226
#endif
213227

214228
/* initialize server, but don't accept connections yet */
215229
if (0 > http_server_init2(&server))
216230
exit(EXIT_FAILURE);
217231

232+
if (port == 0) {
233+
struct vmfile vmf = VMFILE_INITIALIZER;
234+
if (0 > vmfile_init(&vmf, "httpd.port", 4096))
235+
exit(EXIT_FAILURE);
236+
vmfile_sprintf(&vmf, "%d", server.port);
237+
vmfile_close(&vmf);
238+
}
239+
218240
if (0 > ribs_server_init(daemon_mode, "httpd.pid", "httpd.log", forks))
219241
exit(EXIT_FAILURE);
220242

examples/httpget/src/httpget.c

Lines changed: 101 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -28,61 +28,88 @@
2828
#include <sys/stat.h>
2929
#include <fcntl.h>
3030
#include <ctype.h>
31+
#include <unistd.h>
3132

3233
void usage(char *progname) {
3334
dprintf(STDERR_FILENO,
3435
"Usage: %s [options]... <URL>\n"
35-
"\t-o, --outfile <FILE>\tOutput to file. Ignores -n,-h,-H,-s\n"
36-
"\t-c, --cacert <FILE>\tRead Certificate Authorities certificates file, turns on certificate validation\n"
36+
"\t-C, --cacert <FILE>\tRead Certificate Authorities certificates file, turns on certificate validation\n"
37+
"\t-o, --outfile <FILE>\tOutput to file. Below options are ignored\n"
3738
"\t-h, --headers\t\tPrint headers too\n"
3839
"\t-H, -hh, --headers-only\tPrint headers only\n"
3940
"\t-s, --silent\t\tSilent mode. Print nothing\n"
40-
"\t-n, --nreqs\t\tNumber of request\n", progname);
41+
"\t-n, --nreqs\t\tNumber of request\n"
42+
"\t-c, --concurrency\tNuber of concurrent requests\n"
43+
"\t-r, --reconnect\t\tRequest non-persistent\n"
44+
"\t-f, --filedes\t\tFile descritor for data output (default: 1)\n"
45+
"Debugging options:\n"
46+
"\t-p, \t\t\tUse the 'content' pointer instead of vmbuf_write\n"
47+
, progname);
48+
4149
exit(EXIT_FAILURE);
4250
}
4351

4452
int main(int argc, char *argv[]) {
4553

4654
static struct option long_options[] = {
4755
{"outfile", 1, 0, 'o'},
48-
{"cacert", 1, 0, 'c'},
56+
{"cacert", 1, 0, 'C'},
4957
{"headers", 0, 0, 'h'},
5058
{"nreqs", 1, 0, 'n'},
5159
{"silent", 0, 0, 's'},
5260
{"headers-only", 0, 0, 'H'},
61+
{"reconnect", 0, 0, 'r'},
62+
{"concurrency", 1, 0, 'c'},
63+
{"filedes", 1, 0, 'f'},
5364
{0, 0, 0, 0}
5465
};
5566

56-
int silent = 0;
67+
int concurrency = 1;
68+
char silent = 0;
5769
int64_t nreqs = 0;
58-
int headers = 0;
70+
char headers = 0;
5971
char *cacert = NULL;
60-
char *file = NULL;
72+
char *save_to_file = NULL;
73+
char force_close = 0;
74+
char use_content = 0;
75+
int output_fileno = STDOUT_FILENO;
6176

6277
for (;;) {
6378
int option_index = 0;
64-
int c = getopt_long(argc, argv, "o:c:hn:sH", long_options, &option_index);
79+
int c = getopt_long(argc, argv, "o:c:n:C:f:shHrp", long_options, &option_index);
6580
if (c == -1)
6681
break;
6782
switch (c) {
6883
case 'o':
69-
file = optarg;
84+
save_to_file = optarg;
7085
break;
71-
case 'c':
86+
case 'C':
7287
cacert = optarg;
7388
break;
7489
case 'h':
7590
++headers;
7691
break;
7792
case 'n':
78-
nreqs = atoi(optarg);
93+
nreqs = strtoull(optarg, NULL, 10);
7994
break;
8095
case 's':
8196
silent = 1;
8297
break;
8398
case 'H':
8499
headers += 2;
85100
break;
101+
case 'r':
102+
force_close = 1;
103+
break;
104+
case 'c':
105+
concurrency = atoi(optarg);
106+
break;
107+
case 'p':
108+
use_content = 1;
109+
break;
110+
case 'f':
111+
output_fileno = atoi(optarg);
112+
break;
86113
default:
87114
LOGGER_ERROR("Wrong argument:-%c", c);
88115
usage(argv[0]);
@@ -100,7 +127,7 @@ int main(int argc, char *argv[]) {
100127

101128
if (nreqs < 1) nreqs = 1;
102129

103-
if (file) {
130+
if (save_to_file) {
104131
if (nreqs > 1)
105132
LOGGER_INFO("File mode, nreqs will be ignored");
106133
if (silent)
@@ -125,14 +152,14 @@ int main(int argc, char *argv[]) {
125152
if (!strncasecmp("http://", url, 7)) {
126153
url += 7;
127154
} else if (!strncasecmp("https://", url, 8)) {
128-
if (http_client_pool_init_ssl(&client_pool, 1, 1, cacert))
155+
if (http_client_pool_init_ssl(&client_pool, concurrency, 1, cacert))
129156
exit(EXIT_FAILURE);
130157
url += 8;
131158
port = 443;
132159
}
133160

134161
if (80 == port)
135-
if (http_client_pool_init(&client_pool, 1, 1))
162+
if (http_client_pool_init(&client_pool, concurrency, 1))
136163
exit(EXIT_FAILURE);
137164

138165
char *hostandport = strtok(url, "/");
@@ -155,12 +182,12 @@ int main(int argc, char *argv[]) {
155182
LOGGER_ERROR("Could not resolve: %s", host);
156183
exit(EXIT_FAILURE);
157184
}
158-
struct in_addr addr;
159-
addr = *(struct in_addr *)h->h_addr_list[0];
160185

161-
if (file) {
186+
struct in_addr addr = *(struct in_addr *)h->h_addr_list[0];
187+
188+
if (save_to_file) {
162189
struct vmfile infile = VMFILE_INITIALIZER;
163-
if (0 != vmfile_init(&infile, file, 4096)) {
190+
if (0 != vmfile_init(&infile, save_to_file, 4096)) {
164191
LOGGER_PERROR("Error opening file for write");
165192
exit(EXIT_FAILURE);
166193
}
@@ -173,47 +200,65 @@ int main(int argc, char *argv[]) {
173200
vmfile_close(&infile);
174201

175202
} else {
176-
struct http_client_context *rcctx;
177-
LOOP:
178-
rcctx = http_client_pool_create_client2(&client_pool, addr, port, host, NULL);
179-
if (NULL == rcctx) {
180-
LOGGER_PERROR("Error sending request");
181-
exit(EXIT_FAILURE);
182-
}
183-
vmbuf_sprintf(&rcctx->request, "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n", uri, host);
184-
int persistent = 0;
185-
for (; nreqs > 0; --nreqs) {
186-
if (0 > http_client_send_request(rcctx)) {
187-
if (!persistent) --nreqs;
188-
http_client_free(rcctx);
189-
goto LOOP;
203+
void requestor(void) {
204+
struct http_client_context *rcctx;
205+
LOOP:
206+
rcctx = http_client_pool_create_client2(&client_pool, addr, port, host, NULL);
207+
if (NULL == rcctx) {
208+
LOGGER_PERROR("Error sending request");
209+
exit(EXIT_FAILURE);
190210
}
191-
/* Try to read right away, without epoll_wait */
192-
ribs_swapcurcontext(RIBS_RESERVED_TO_CONTEXT(rcctx));
193-
//yield();
194-
/* rcctx = http_client_get_last_context(); */
195-
if (!silent) {
196-
if (headers > 1)
197-
vmbuf_wlocset(&rcctx->response, vmbuf_rlocpos(&rcctx->response)-4);
198-
if (headers)
199-
vmbuf_rlocset(&rcctx->response, 0);
200-
vmbuf_write(&rcctx->response, STDOUT_FILENO);
201-
printf("\n");
202-
}
203-
/* Not persistent? */
204-
if (!rcctx->persistent) {
211+
rcctx->persistent = 1;
212+
vmbuf_sprintf(&rcctx->request, "GET /%s HTTP/1.1\r\nHost: %s\r\n", uri, host);
213+
if (force_close)
214+
vmbuf_strcpy(&rcctx->request, "Connection: close\r\n\r\n");
215+
else
216+
vmbuf_strcpy(&rcctx->request, "\r\n");
217+
while(nreqs > 0) {
205218
--nreqs;
206-
http_client_free(rcctx);
207-
goto LOOP;
219+
/* attempt to write and read right away, without epoll_wait */
220+
ribs_swapcurcontext(RIBS_RESERVED_TO_CONTEXT(rcctx));
221+
if (!silent) {
222+
if (headers > 1)
223+
vmbuf_wlocset(&rcctx->response, vmbuf_rlocpos(&rcctx->response)-4);
224+
if (headers)
225+
vmbuf_rlocset(&rcctx->response, 0);
226+
if (STDOUT_FILENO == output_fileno)
227+
vmbuf_chrcpy(&rcctx->response, '\n');
228+
if (use_content && rcctx->content) {
229+
vmbuf_chrcpy(&rcctx->response, '\0');
230+
dprintf(output_fileno, "%s", rcctx->content);
231+
} else
232+
vmbuf_write(&rcctx->response, output_fileno);
233+
}
234+
/* Not persistent? */
235+
if (!rcctx->persistent) {
236+
http_client_free(rcctx);
237+
goto LOOP;
238+
}
239+
/* clear the response buffer */
240+
vmbuf_reset(&rcctx->response);
241+
/* rollback and reuse the request */
242+
vmbuf_rreset(&rcctx->request);
243+
/* reset the fiber */
244+
http_client_reuse_context(rcctx);
245+
}
246+
if (rcctx->persistent) {
247+
rcctx->persistent = 0;
248+
ribs_close(rcctx->fd);
208249
}
209-
persistent = 1;
210-
/* clear the response buffer */
211-
vmbuf_reset(&rcctx->response);
212-
/* rollback and reuse the request */
213-
vmbuf_rreset(&rcctx->request);
214-
/* reset the fiber */
215-
http_client_reuse_context(rcctx);
216-
}
250+
http_client_free(rcctx);
251+
}
252+
253+
int i;
254+
for (i = 0; i < concurrency; ++i) {
255+
struct ribs_context *ctx = ribs_context_create(1024 * 1024, 0, requestor);
256+
queue_current_ctx();
257+
ribs_swapcurcontext(ctx);
258+
}
259+
for (i = 0; i < concurrency; ++i) {
260+
yield();
261+
}
217262
}
218263
return 0;
219264
}

include/epoll_worker.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ int ribs_epoll_add(int fd, uint32_t events, struct ribs_context* ctx);
4444
struct ribs_context* small_ctx_for_fd(int fd, size_t reserved_size, void (*func)(void));
4545
int queue_current_ctx(void);
4646
int epoll_close();
47+
int ribs_close(int fd);
4748

4849
_RIBS_INLINE_ void epoll_worker_ignore_events(int fd);
4950
_RIBS_INLINE_ void epoll_worker_resume_events(int fd);
5051
_RIBS_INLINE_ void epoll_worker_set_fd_ctx(int fd, struct ribs_context* ctx);
5152
_RIBS_INLINE_ void epoll_worker_set_last_fd(int fd);
52-
_RIBS_INLINE_ int ribs_close(int fd);
53+
5354

5455
#include "../src/_epoll_worker.c"
5556

include/hash_funcs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
#ifndef _HASH_FUNCS__H_
2121
#define _HASH_FUNCS__H_
2222

23-
#if 0
24-
static inline uint32_t hashcode(const void *key, size_t n) {
23+
24+
static inline uint32_t hashcode2(const void *key, size_t n) {
2525
register const unsigned char *p = (const unsigned char *)key;
2626
register const unsigned char *end = p + n;
2727
uint32_t h = 5381;
2828
for (; p != end; ++p)
2929
h = ((h << 5) + h) ^ *p;
3030
return h;
3131
}
32-
#endif
32+
3333
static inline uint32_t hashcode(const void *key, size_t n)
3434
{
3535
register const unsigned char *p = (const unsigned char *)key;

include/http_client_pool.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ struct http_client_pool {
4040

4141
struct http_client_context {
4242
int fd;
43+
uint32_t content_length;
4344
struct vmbuf request;
4445
struct vmbuf response;
45-
epoll_data_t userdata;
46-
int persistent;
47-
int http_status_code;
4846
char *content;
49-
uint32_t content_length;
5047
struct http_client_pool *pool;
48+
epoll_data_t userdata;
49+
short http_status_code;
5150
#ifdef RIBS2_SSL
52-
int ssl_connected;
5351
const char *hostname;
52+
char ssl_connected;
5453
#endif
54+
char persistent;
5555
/* TODO: add initial buffer sizes */
5656

5757
/* keep 1 byte aligned structs last */
@@ -74,7 +74,7 @@ int http_client_pool_get_request(struct http_client_pool *http_client_pool, stru
7474
int http_client_pool_get_request2(struct http_client_pool *http_client_pool, struct in_addr addr, uint16_t port, const char *hostname, const char **headers, const char *format, ...) __attribute__ ((format (gnu_printf, 6, 7)));
7575
int http_client_pool_post_request(struct http_client_pool *http_client_pool, struct in_addr addr, uint16_t port, const char *hostname, const char *data, size_t size_of_data, const char *format, ...) __attribute__ ((format (gnu_printf, 7, 8)));
7676
struct http_client_context *http_client_pool_post_request_init(struct http_client_pool *http_client_pool, struct in_addr addr, uint16_t port, const char *hostname, const char *format, ...) __attribute__ ((format (gnu_printf, 5, 6)));
77-
inline int http_client_pool_post_request_content_type(struct http_client_context *context, const char *content_type);
77+
inline int http_client_pool_post_request_content_type(struct http_client_context *cctx, const char *content_type);
7878
int http_client_pool_post_request_send(struct http_client_context *context, struct vmbuf *post_data);
7979
int http_client_get_file(struct http_client_pool *http_client_pool, struct vmfile *infile, struct in_addr addr, uint16_t port, const char *hostname, int compression, int * file_compressed, const char *format, ...) __attribute__ ((format (gnu_printf, 8, 9)));
8080
struct http_client_context *http_client_get_last_context(void);

include/http_headers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct http_headers{
3737
char *if_none_match;
3838
char *accept_language;
3939
char *origin;
40+
char *authorization;
4041
uint8_t accept_encoding_mask;
4142
char peer_ip_addr[INET_ADDRSTRLEN];
4243
};

include/ribs_zlib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
int vmbuf_deflate(struct vmbuf *buf);
2727
int vmbuf_deflate2(struct vmbuf *inbuf, struct vmbuf *outbuf);
28+
int vmbuf_deflate3(struct vmbuf *buf, int level);
29+
int vmbuf_deflate4(struct vmbuf *inbuf, struct vmbuf *outbuf, int level);
2830
int vmbuf_inflate(struct vmbuf *buf);
2931
int vmbuf_inflate2(struct vmbuf *inbuf, struct vmbuf *outbuf);
3032
int vmbuf_inflate_gzip(void *inbuf, size_t in_size, struct vmbuf *outbuf);

0 commit comments

Comments
 (0)