proxygen
test.c File Reference
#include "http_parser.h"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

Go to the source code of this file.

Classes

struct  message
 
struct  url_test
 

Macros

#define TRUE   1
 
#define FALSE   0
 
#define MAX_HEADERS   13
 
#define MAX_ELEMENT_SIZE   500
 
#define MAX_CHUNKS   16
 
#define MIN(a, b)   ((a) < (b) ? (a) : (b))
 
#define CURL_GET   0
 
#define FIREFOX_GET   1
 
#define DUMBFUCK   2
 
#define FRAGMENT_IN_URI   3
 
#define GET_NO_HEADERS_NO_BODY   4
 
#define GET_ONE_HEADER_NO_BODY   5
 
#define GET_FUNKY_CONTENT_LENGTH   6
 
#define POST_IDENTITY_BODY_WORLD   7
 
#define POST_CHUNKED_ALL_YOUR_BASE   8
 
#define TWO_CHUNKS_MULT_ZERO_END   9
 
#define CHUNKED_W_TRAILING_HEADERS   10
 
#define CHUNKED_W_BULLSHIT_AFTER_LENGTH   11
 
#define WITH_QUOTES   12
 
#define APACHEBENCH_GET   13
 
#define QUERY_URL_WITH_QUESTION_MARK_GET   14
 
#define PREFIX_NEWLINE_GET   15
 
#define UPGRADE_REQUEST   16
 
#define CONNECT_REQUEST   17
 
#define REPORT_REQ   18
 
#define NO_HTTP_VERSION   19
 
#define MSEARCH_REQ   20
 
#define LINE_FOLDING_IN_HEADER   20
 
#define QUERY_TERMINATED_HOST   21
 
#define QUERY_TERMINATED_HOSTPORT   22
 
#define SPACE_TERMINATED_HOSTPORT   23
 
#define UTF8_PATH_REQ   24
 
#define HOSTNAME_UNDERSCORE   25
 
#define PATCH_REQ   26
 
#define CONNECT_CAPS_REQUEST   27
 
#define IPV6_LITERAL_URI_GET   28
 
#define IPV6_LITERAL_URI_CONNECT   29
 
#define UPGRADE_POST_REQUEST   30
 
#define CONNECT_WITH_BODY_REQUEST   31
 
#define GOOGLE_301   0
 
#define NO_CONTENT_LENGTH_RESPONSE   1
 
#define NO_HEADERS_NO_BODY_404   2
 
#define NO_REASON_PHRASE   3
 
#define TRAILING_SPACE_ON_CHUNKED_BODY   4
 
#define NO_CARRIAGE_RET   5
 
#define PROXY_CONNECTION   6
 
#define UNDERSTORE_HEADER_KEY   7
 
#define BONJOUR_MADAME_FR   8
 
#define RES_FIELD_UNDERSCORE   10
 
#define NON_ASCII_IN_STATUS_LINE   11
 
#define MESSAGE_CHECK_STR_EQ(expected, found, prop)   if (!check_str_eq(expected, #prop, expected->prop, found->prop)) return 0
 
#define MESSAGE_CHECK_NUM_EQ(expected, found, prop)   if (!check_num_eq(expected, #prop, expected->prop, found->prop)) return 0
 

Functions

int request_url_cb (http_parser *p, const char *buf, size_t len)
 
int header_field_cb (http_parser *p, const char *buf, size_t len)
 
int header_value_cb (http_parser *p, const char *buf, size_t len)
 
int body_cb (http_parser *p, const char *buf, size_t len)
 
int count_body_cb (http_parser *p, const char *buf, size_t len)
 
int message_begin_cb (http_parser *p)
 
int headers_complete_cb (http_parser *p, const char *buf, size_t len)
 
int message_complete_cb (http_parser *p)
 
int response_reason_cb (http_parser *p, const char *buf, size_t len)
 
int chunk_header_cb (http_parser *p)
 
int chunk_complete_cb (http_parser *p)
 
int dontcall_message_begin_cb (http_parser *p)
 
int dontcall_header_field_cb (http_parser *p, const char *buf, size_t len)
 
int dontcall_header_value_cb (http_parser *p, const char *buf, size_t len)
 
int dontcall_request_url_cb (http_parser *p, const char *buf, size_t len)
 
int dontcall_body_cb (http_parser *p, const char *buf, size_t len)
 
int dontcall_headers_complete_cb (http_parser *p, const char *buf, size_t len)
 
int dontcall_message_complete_cb (http_parser *p)
 
int dontcall_response_reason_cb (http_parser *p, const char *buf, size_t len)
 
int dontcall_chunk_header_cb (http_parser *p)
 
int dontcall_chunk_complete_cb (http_parser *p)
 
int pause_message_begin_cb (http_parser *p)
 
int pause_header_field_cb (http_parser *p, const char *buf, size_t len)
 
int pause_header_value_cb (http_parser *p, const char *buf, size_t len)
 
int pause_request_url_cb (http_parser *p, const char *buf, size_t len)
 
int pause_body_cb (http_parser *p, const char *buf, size_t len)
 
int pause_headers_complete_cb (http_parser *p, const char *buf, size_t len)
 
int pause_message_complete_cb (http_parser *p)
 
int pause_response_reason_cb (http_parser *p, const char *buf, size_t len)
 
int pause_chunk_header_cb (http_parser *p)
 
int pause_chunk_complete_cb (http_parser *p)
 
int empty_cb (http_parser *p)
 
int empty_data_cb (http_parser *p, const char *buf, size_t len)
 
void parser_init (enum http_parser_type type)
 
void parser_free ()
 
size_t parse (const char *buf, size_t len)
 
size_t parse_count_body (const char *buf, size_t len)
 
size_t parse_pause (const char *buf, size_t len)
 
static int check_str_eq (const struct message *m, const char *prop, const char *expected, const char *found)
 
static int check_num_eq (const struct message *m, const char *prop, int expected, int found)
 
int message_eq (int index, const struct message *expected)
 
size_t count_parsed_messages (const size_t nmsgs,...)
 
void upgrade_message_fix (char *body, const size_t nread, const size_t nmsgs,...)
 
static void print_error (const char *raw, size_t error_location)
 
void dump_url (const char *url, const struct http_parser_url *u)
 
void test_parse_url (void)
 
void test_message (const struct message *message)
 
void test_message_count_body (const struct message *message)
 
void test_simple (const char *buf, enum http_errno err_expected)
 
void test_header_overflow_error (int req)
 
void test_no_overflow_long_body (int req, size_t length)
 
void test_multiple3 (const struct message *r1, const struct message *r2, const struct message *r3)
 
void test_scan (const struct message *r1, const struct message *r2, const struct message *r3)
 
char * create_large_chunked_message (int body_size_in_kb, const char *headers)
 
void test_message_pause (const struct message *msg)
 
void test_on_message_begin_cb (const struct message *message)
 
int main (void)
 

Variables

static http_parserparser
 
static int currently_parsing_eof
 
static struct message messages [5]
 
static int num_messages
 
static http_parser_settingscurrent_pause_parser
 
const struct message requests []
 
const struct message responses []
 
const struct message on_message_begin_cb_test []
 
static http_parser_settings settings_dontcall
 
static http_parser_settings settings_pause
 
static http_parser_settings settings
 
static http_parser_settings settings_count_body
 
static http_parser_settings settings_null
 
const struct url_test url_tests []
 

Macro Definition Documentation

#define APACHEBENCH_GET   13
#define BONJOUR_MADAME_FR   8

Referenced by main().

#define CHUNKED_W_BULLSHIT_AFTER_LENGTH   11

Referenced by main().

#define CHUNKED_W_TRAILING_HEADERS   10

Referenced by main().

#define CONNECT_CAPS_REQUEST   27
#define CONNECT_REQUEST   17

Referenced by main().

#define CONNECT_WITH_BODY_REQUEST   31
#define CURL_GET   0
#define DUMBFUCK   2
#define FIREFOX_GET   1
#define FRAGMENT_IN_URI   3
#define GET_FUNKY_CONTENT_LENGTH   6

Referenced by main().

#define GET_NO_HEADERS_NO_BODY   4

Referenced by main().

#define GET_ONE_HEADER_NO_BODY   5

Referenced by main().

#define GOOGLE_301   0
#define HOSTNAME_UNDERSCORE   25
#define IPV6_LITERAL_URI_CONNECT   29
#define IPV6_LITERAL_URI_GET   28
#define LINE_FOLDING_IN_HEADER   20
#define MAX_CHUNKS   16

Definition at line 36 of file test.c.

Referenced by chunk_header_cb(), main(), and message_eq().

#define MAX_ELEMENT_SIZE   500

Definition at line 35 of file test.c.

Referenced by header_value_cb().

#define MAX_HEADERS   13

Definition at line 34 of file test.c.

#define MESSAGE_CHECK_NUM_EQ (   expected,
  found,
  prop 
)    if (!check_num_eq(expected, #prop, expected->prop, found->prop)) return 0

Definition at line 1655 of file test.c.

Referenced by message_eq().

#define MESSAGE_CHECK_STR_EQ (   expected,
  found,
  prop 
)    if (!check_str_eq(expected, #prop, expected->prop, found->prop)) return 0

Definition at line 1652 of file test.c.

Referenced by message_eq().

#define MIN (   a,
  b 
)    ((a) < (b) ? (a) : (b))

Definition at line 38 of file test.c.

Referenced by test_message_count_body().

#define MSEARCH_REQ   20
#define NO_CARRIAGE_RET   5

Referenced by main().

#define NO_CONTENT_LENGTH_RESPONSE   1
#define NO_HEADERS_NO_BODY_404   2

Referenced by main().

#define NO_HTTP_VERSION   19
#define NO_REASON_PHRASE   3

Referenced by main().

#define NON_ASCII_IN_STATUS_LINE   11
#define PATCH_REQ   26
#define POST_CHUNKED_ALL_YOUR_BASE   8

Referenced by main().

#define POST_IDENTITY_BODY_WORLD   7

Referenced by main().

#define PREFIX_NEWLINE_GET   15

Referenced by main().

#define PROXY_CONNECTION   6
#define QUERY_TERMINATED_HOST   21
#define QUERY_TERMINATED_HOSTPORT   22
#define QUERY_URL_WITH_QUESTION_MARK_GET   14

Referenced by main().

#define REPORT_REQ   18
#define RES_FIELD_UNDERSCORE   10
#define SPACE_TERMINATED_HOSTPORT   23
#define TRAILING_SPACE_ON_CHUNKED_BODY   4

Referenced by main().

#define TWO_CHUNKS_MULT_ZERO_END   9

Referenced by main().

#define UNDERSTORE_HEADER_KEY   7

Referenced by main().

#define UPGRADE_POST_REQUEST   30
#define UPGRADE_REQUEST   16
#define UTF8_PATH_REQ   24
#define WITH_QUOTES   12

Function Documentation

int body_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1246 of file test.c.

References message::body, message::body_size, messages, and num_messages.

Referenced by pause_body_cb().

1247 {
1248  assert(p == parser);
1249  strncat(messages[num_messages].body, buf, len);
1251  // printf("body_cb: '%s'\n", requests[num_messages].body);
1252  return 0;
1253 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
static http_parser * parser
Definition: test.c:40
size_t body_size
Definition: test.c:52
static int check_num_eq ( const struct message m,
const char *  prop,
int  expected,
int  found 
)
inlinestatic

Definition at line 1639 of file test.c.

References message::name.

1642  {
1643  if (expected != found) {
1644  printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
1645  printf("expected %d\n", expected);
1646  printf(" found %d\n", found);
1647  return 0;
1648  }
1649  return 1;
1650 }
const char * name
Definition: test.c:43
static int check_str_eq ( const struct message m,
const char *  prop,
const char *  expected,
const char *  found 
)
inlinestatic

Definition at line 1619 of file test.c.

References message::name.

Referenced by message_eq(), and upgrade_message_fix().

1622  {
1623  if ((expected == NULL) != (found == NULL)) {
1624  printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
1625  printf("expected %s\n", (expected == NULL) ? "NULL" : expected);
1626  printf(" found %s\n", (found == NULL) ? "NULL" : found);
1627  return 0;
1628  }
1629  if (expected != NULL && 0 != strcmp(expected, found)) {
1630  printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name);
1631  printf("expected '%s'\n", expected);
1632  printf(" found '%s'\n", found);
1633  return 0;
1634  }
1635  return 1;
1636 }
const char * name
Definition: test.c:43
int chunk_complete_cb ( http_parser p)

Definition at line 1324 of file test.c.

References messages, message::num_chunks_complete, and num_messages.

Referenced by pause_chunk_complete_cb().

1325 {
1326  assert(p == parser);
1328  return 0;
1329 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
static http_parser * parser
Definition: test.c:40
int num_chunks_complete
Definition: test.c:59
int chunk_header_cb ( http_parser p)

Definition at line 1306 of file test.c.

References message::chunk_lengths, http_parser::content_length, MAX_CHUNKS, messages, message::num_chunks, and num_messages.

Referenced by pause_chunk_header_cb().

1307 {
1308  assert(p == parser);
1309  if (p->content_length == 0) {
1310  // Terminating chunk. Don't increment num_chunks in this case.
1311  return 0;
1312  }
1313 
1314  int chunk_idx = messages[num_messages].num_chunks;
1316  if (chunk_idx < MAX_CHUNKS) {
1318  }
1319 
1320  return 0;
1321 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
int chunk_lengths[16]
Definition: test.c:60
#define MAX_CHUNKS
Definition: test.c:36
static http_parser * parser
Definition: test.c:40
int num_chunks
Definition: test.c:58
int64_t content_length
Definition: http_parser.h:221
int count_body_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1256 of file test.c.

References message::body_size, messages, and num_messages.

1257 {
1258  assert(p == parser);
1259  assert(buf);
1261  return 0;
1262 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
static http_parser * parser
Definition: test.c:40
size_t body_size
Definition: test.c:52
size_t count_parsed_messages ( const size_t  nmsgs,
  ... 
)

Definition at line 1715 of file test.c.

References i, m, and message::upgrade.

Referenced by test_multiple3(), and test_scan().

1715  {
1716  size_t i;
1717  va_list ap;
1718 
1719  va_start(ap, nmsgs);
1720 
1721  for (i = 0; i < nmsgs; i++) {
1722  struct message *m = va_arg(ap, struct message *);
1723 
1724  if (m->upgrade) {
1725  va_end(ap);
1726  return i + 1;
1727  }
1728  }
1729 
1730  va_end(ap);
1731  return nmsgs;
1732 }
Definition: test.c:42
const char * upgrade
Definition: test.c:62
static map< string, int > m
char* create_large_chunked_message ( int  body_size_in_kb,
const char *  headers 
)

Definition at line 2740 of file test.c.

References i.

Referenced by main().

2741 {
2742  int i;
2743  size_t wrote = 0;
2744  size_t headers_len = strlen(headers);
2745  size_t bufsize = headers_len + (5+1024+2)*body_size_in_kb + 6;
2746  char * buf = malloc(bufsize);
2747 
2748  memcpy(buf, headers, headers_len);
2749  wrote += headers_len;
2750 
2751  for (i = 0; i < body_size_in_kb; i++) {
2752  // write 1kb chunk into the body.
2753  memcpy(buf + wrote, "400\r\n", 5);
2754  wrote += 5;
2755  memset(buf + wrote, 'C', 1024);
2756  wrote += 1024;
2757  strcpy(buf + wrote, "\r\n");
2758  wrote += 2;
2759  }
2760 
2761  memcpy(buf + wrote, "0\r\n\r\n", 6);
2762  wrote += 6;
2763  assert(wrote == bufsize);
2764 
2765  return buf;
2766 }
char headers[13][2][500]
Definition: test.c:55
int dontcall_body_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1366 of file test.c.

1367 {
1368  if (p || buf || len) { } // gcc
1369  fprintf(stderr, "\n\n*** on_body_cb() called on paused parser ***\n\n");
1370  exit(1);
1371 }
int dontcall_chunk_complete_cb ( http_parser p)

Definition at line 1408 of file test.c.

1409 {
1410  if (p) { } // gcc
1411  fprintf(stderr, "\n\n*** on_chunk_complete() "
1412  "called on paused parser ***\n\n");
1413  exit(1);
1414 }
int dontcall_chunk_header_cb ( http_parser p)

Definition at line 1400 of file test.c.

1401 {
1402  if (p) { } // gcc
1403  fprintf(stderr, "\n\n*** on_chunk_header() called on paused parser ***\n\n");
1404  exit(1);
1405 }
int dontcall_header_field_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1342 of file test.c.

1343 {
1344  if (p || buf || len) { } // gcc
1345  fprintf(stderr, "\n\n*** on_header_field() called on paused parser ***\n\n");
1346  exit(1);
1347 }
int dontcall_header_value_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1350 of file test.c.

1351 {
1352  if (p || buf || len) { } // gcc
1353  fprintf(stderr, "\n\n*** on_header_value() called on paused parser ***\n\n");
1354  exit(1);
1355 }
int dontcall_headers_complete_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1374 of file test.c.

1375 {
1376  if (p || buf || len) { } // gcc
1377  fprintf(stderr, "\n\n*** on_headers_complete() called on paused "
1378  "parser ***\n\n");
1379  exit(1);
1380 }
int dontcall_message_begin_cb ( http_parser p)

Definition at line 1334 of file test.c.

1335 {
1336  if (p) { } // gcc
1337  fprintf(stderr, "\n\n*** on_message_begin() called on paused parser ***\n\n");
1338  exit(1);
1339 }
int dontcall_message_complete_cb ( http_parser p)

Definition at line 1383 of file test.c.

1384 {
1385  if (p) { } // gcc
1386  fprintf(stderr, "\n\n*** on_message_complete() called on paused "
1387  "parser ***\n\n");
1388  exit(1);
1389 }
int dontcall_request_url_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1358 of file test.c.

1359 {
1360  if (p || buf || len) { } // gcc
1361  fprintf(stderr, "\n\n*** on_request_url() called on paused parser ***\n\n");
1362  exit(1);
1363 }
int dontcall_response_reason_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1392 of file test.c.

1393 {
1394  if (p || buf || len) { } // gcc
1395  fprintf(stderr, "\n\n*** on_reason() called on paused parser ***\n\n");
1396  exit(1);
1397 }
void dump_url ( const char *  url,
const struct http_parser_url u 
)

Definition at line 2309 of file test.c.

References http_parser_url::field_data, http_parser_url::field_set, i, http_parser_url::len, http_parser_url::off, http_parser_url::port, and UF_MAX.

Referenced by test_parse_url().

2310 {
2311  unsigned int i;
2312 
2313  printf("\tfield_set: 0x%x, port: %u\n", u->field_set, u->port);
2314  for (i = 0; i < UF_MAX; i++) {
2315  if ((u->field_set & (1 << i)) == 0) {
2316  printf("\tfield_data[%u]: unset\n", i);
2317  continue;
2318  }
2319 
2320  printf("\tfield_data[%u]: off: %u len: %u part: \"%.*s\n\"",
2321  i,
2322  u->field_data[i].off,
2323  u->field_data[i].len,
2324  u->field_data[i].len,
2325  url + u->field_data[i].off);
2326  }
2327 }
uint16_t field_set
Definition: http_parser.h:283
struct http_parser_url::@1 field_data[UF_MAX]
int empty_cb ( http_parser p)

Definition at line 1513 of file test.c.

1513 { return 0; }
int empty_data_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1514 of file test.c.

1514 { return 0; }
int header_field_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1212 of file test.c.

References message::FIELD, message::headers, message::last_header_element, m, messages, message::num_headers, and num_messages.

Referenced by pause_header_field_cb().

1213 {
1214  assert(p == parser);
1215  struct message *m = &messages[num_messages];
1216 
1217  if (m->last_header_element != FIELD)
1218  m->num_headers++;
1219 
1220  strncat(m->headers[m->num_headers-1][0], buf, len);
1221 
1223 
1224  return 0;
1225 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
Definition: test.c:42
enum message::@3 last_header_element
static http_parser * parser
Definition: test.c:40
static map< string, int > m
char headers[13][2][500]
Definition: test.c:55
int num_headers
Definition: test.c:53
int header_value_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1228 of file test.c.

References message::headers, message::last_header_element, m, MAX_ELEMENT_SIZE, messages, message::num_headers, num_messages, and message::VALUE.

Referenced by pause_header_value_cb().

1229 {
1230  assert(p == parser);
1231  struct message *m = &messages[num_messages];
1232 
1233  const size_t cursize = strlen(m->headers[m->num_headers-1][1]);
1234  if (cursize + len + 1 > MAX_ELEMENT_SIZE) {
1235  len = MAX_ELEMENT_SIZE - (cursize + 1);
1236  }
1237 
1238  strncat(m->headers[m->num_headers-1][1], buf, len);
1239 
1241 
1242  return 0;
1243 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
Definition: test.c:42
enum message::@3 last_header_element
static http_parser * parser
Definition: test.c:40
#define MAX_ELEMENT_SIZE
Definition: test.c:35
static map< string, int > m
char headers[13][2][500]
Definition: test.c:55
int num_headers
Definition: test.c:53
int headers_complete_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1273 of file test.c.

References message::headers_complete_cb_called, message::http_major, http_parser::http_major, message::http_minor, http_parser::http_minor, messages, message::method, http_parser::method, num_messages, message::status_code, http_parser::status_code, and TRUE.

Referenced by pause_headers_complete_cb().

1274 {
1275  assert(p == parser);
1276  assert(!buf);
1282  return 0;
1283 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
unsigned short http_minor
Definition: http_parser.h:225
enum http_method method
Definition: test.c:46
int headers_complete_cb_called
Definition: test.c:68
unsigned short http_major
Definition: test.c:64
static http_parser * parser
Definition: test.c:40
#define TRUE
Definition: test.c:30
unsigned short status_code
Definition: http_parser.h:226
int status_code
Definition: test.c:47
unsigned char method
Definition: http_parser.h:227
unsigned short http_major
Definition: http_parser.h:224
unsigned short http_minor
Definition: test.c:65
int main ( void  )

TESTING ON_MESSAGE_BEGIN CALLS

REQUESTS

Definition at line 2834 of file test.c.

References BONJOUR_MADAME_FR, message::chunk_lengths, CHUNKED_W_BULLSHIT_AFTER_LENGTH, CHUNKED_W_TRAILING_HEADERS, CONNECT_REQUEST, create_large_chunked_message(), FALSE, bm::free(), GET_FUNKY_CONTENT_LENGTH, GET_NO_HEADERS_NO_BODY, GET_ONE_HEADER_NO_BODY, HPE_INVALID_CONTENT_LENGTH, HPE_INVALID_HEADER_TOKEN, HPE_INVALID_METHOD, HPE_INVALID_TRANSFER_ENCODING, HPE_INVALID_UPGRADE, HPE_INVALID_VERSION, HPE_OK, HPE_STRICT, HPE_UNKNOWN, HTTP_REQUEST, HTTP_RESPONSE, i, k, MAX_CHUNKS, message::name, NO_CARRIAGE_RET, NO_HEADERS_NO_BODY_404, NO_REASON_PHRASE, POST_CHUNKED_ALL_YOUR_BASE, POST_IDENTITY_BODY_WORLD, PREFIX_NEWLINE_GET, QUERY_URL_WITH_QUESTION_MARK_GET, message::should_keep_alive, test_header_overflow_error(), test_message(), test_message_count_body(), test_message_pause(), test_multiple3(), test_no_overflow_long_body(), test_on_message_begin_cb(), test_parse_url(), test_scan(), test_simple(), TRAILING_SPACE_ON_CHUNKED_BODY, TWO_CHUNKS_MULT_ZERO_END, and UNDERSTORE_HEADER_KEY.

Referenced by cmake_minimum_required().

2835 {
2836  parser = NULL;
2837  int i, j, k;
2838  int request_count;
2839  int response_count;
2840  int on_message_begin_cb_count;
2841 
2842  printf("sizeof(http_parser) = %u\n", (unsigned int)sizeof(http_parser));
2843 
2844  for (request_count = 0; requests[request_count].name; request_count++);
2845  for (response_count = 0; responses[response_count].name; response_count++);
2846  for (on_message_begin_cb_count = 0;
2847  on_message_begin_cb_test[on_message_begin_cb_count].name;
2848  on_message_begin_cb_count++);
2849 
2851  test_parse_url();
2852 
2854  for (i = 0 ; i < on_message_begin_cb_count; i++) {
2856  }
2857 
2859 
2863 
2867 
2869 
2870  for (i = 0; i < response_count; i++) {
2871  test_message(&responses[i]);
2872  }
2873 
2874  for (i = 0; i < response_count; i++) {
2876  }
2877 
2878  for (i = 0; i < response_count; i++) {
2879  if (!responses[i].should_keep_alive) continue;
2880  for (j = 0; j < response_count; j++) {
2881  if (!responses[j].should_keep_alive) continue;
2882  for (k = 0; k < response_count; k++) {
2884  }
2885  }
2886  }
2887 
2890 
2891  // test very large chunked response
2892  {
2893  char * msg = create_large_chunked_message(31337,
2894  "HTTP/1.0 200 OK\r\n"
2895  "Transfer-Encoding: chunked\r\n"
2896  "Content-Type: text/plain\r\n"
2897  "\r\n");
2898  struct message large_chunked =
2899  {.name= "large chunked"
2900  ,.type= HTTP_RESPONSE
2901  ,.raw= msg
2902  ,.should_keep_alive= FALSE
2903  ,.message_complete_on_eof= FALSE
2904  ,.http_major= 1
2905  ,.http_minor= 0
2906  ,.status_code= 200
2907  ,.response_reason= "OK"
2908  ,.num_headers= 2
2909  ,.headers=
2910  { { "Transfer-Encoding", "chunked" }
2911  , { "Content-Type", "text/plain" }
2912  }
2913  ,.body_size= 31337*1024
2914  ,.num_chunks= 31337
2915  ,.num_chunks_complete= 31338
2916  };
2917  for (i = 0; i < MAX_CHUNKS; ++i) {
2918  large_chunked.chunk_lengths[i] = 1024;
2919  }
2920  test_message_count_body(&large_chunked);
2921  free(msg);
2922  }
2923 
2924 
2925 
2926  printf("response scan 1/2 ");
2930  );
2931 
2932  printf("response scan 2/2 ");
2936  );
2937 
2938  puts("responses okay");
2939 
2940 
2942 
2943  test_simple("hello world", HPE_INVALID_METHOD);
2944  test_simple("GET / HTP/1.1\r\n\r\n", HPE_INVALID_VERSION);
2945 
2946 
2947  test_simple("ASDF / HTTP/1.1\r\n\r\n", HPE_INVALID_METHOD);
2948  test_simple("PROPPATCHA / HTTP/1.1\r\n\r\n", HPE_INVALID_METHOD);
2949  test_simple("GETA / HTTP/1.1\r\n\r\n", HPE_INVALID_METHOD);
2950 
2951  // Well-formed but incomplete
2952  test_simple("GET / HTTP/1.1\r\n"
2953  "Content-Type: text/plain\r\n"
2954  "Content-Length: 6\r\n"
2955  "\r\n"
2956  "fooba",
2957  HPE_OK);
2958 
2959  static const char *all_methods[] = {
2960  "DELETE",
2961  "GET",
2962  "HEAD",
2963  "POST",
2964  "PUT",
2965  //"CONNECT", //CONNECT can't be tested like other methods, it's a tunnel
2966  "OPTIONS",
2967  "TRACE",
2968  "COPY",
2969  "LOCK",
2970  "MKCOL",
2971  "MOVE",
2972  "PROPFIND",
2973  "PROPPATCH",
2974  "UNLOCK",
2975  "REPORT",
2976  "MKACTIVITY",
2977  "CHECKOUT",
2978  "MERGE",
2979  "M-SEARCH",
2980  "NOTIFY",
2981  "SUBSCRIBE",
2982  "UNSUBSCRIBE",
2983  "PATCH",
2984  0 };
2985  const char **this_method;
2986  for (this_method = all_methods; *this_method; this_method++) {
2987  char buf[200];
2988  sprintf(buf, "%s / HTTP/1.1\r\n\r\n", *this_method);
2989  test_simple(buf, HPE_OK);
2990  }
2991 
2992  static const char *bad_methods[] = {
2993  "C******",
2994  "M****",
2995  0 };
2996  for (this_method = bad_methods; *this_method; this_method++) {
2997  char buf[200];
2998  sprintf(buf, "%s / HTTP/1.1\r\n\r\n", *this_method);
2999  test_simple(buf, HPE_UNKNOWN);
3000  }
3001 
3002  const char *dumbfuck2 =
3003  "GET / HTTP/1.1\r\n"
3004  "X-SSL-Bullshit: -----BEGIN CERTIFICATE-----\r\n"
3005  "\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx\r\n"
3006  "\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT\r\n"
3007  "\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu\r\n"
3008  "\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV\r\n"
3009  "\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV\r\n"
3010  "\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB\r\n"
3011  "\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF\r\n"
3012  "\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR\r\n"
3013  "\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL\r\n"
3014  "\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP\r\n"
3015  "\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5jrgu2SqR\r\n"
3016  "\twgA7BUi3G8LFzMBl8FRCDYGUDy7M6QaHXx1ZWIPWNKsCAwEAAaOCAiQwggIgMAwG\r\n"
3017  "\tA1UdEwEB/wQCMAAwEQYJYIZIAYb4QgHTTPAQDAgWgMA4GA1UdDwEB/wQEAwID6DAs\r\n"
3018  "\tBglghkgBhvhCAQ0EHxYdVUsgZS1TY2llbmNlIFVzZXIgQ2VydGlmaWNhdGUwHQYD\r\n"
3019  "\tVR0OBBYEFDTt/sf9PeMaZDHkUIldrDYMNTBZMIGaBgNVHSMEgZIwgY+AFAI4qxGj\r\n"
3020  "\tloCLDdMVKwiljjDastqooXSkcjBwMQswCQYDVQQGEwJVSzERMA8GA1UEChMIZVNj\r\n"
3021  "\taWVuY2UxEjAQBgNVBAsTCUF1dGhvcml0eTELMAkGA1UEAxMCQ0ExLTArBgkqhkiG\r\n"
3022  "\t9w0BCQEWHmNhLW9wZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE\r\n"
3023  "\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO\r\n"
3024  "\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1\r\n"
3025  "\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0\r\n"
3026  "\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD\r\n"
3027  "\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv\r\n"
3028  "\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3\r\n"
3029  "\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8\r\n"
3030  "\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk\r\n"
3031  "\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK\r\n"
3032  "\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu\r\n"
3033  "\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3\r\n"
3034  "\tRA==\r\n"
3035  "\t-----END CERTIFICATE-----\r\n"
3036  "\r\n";
3037  test_simple(dumbfuck2, HPE_OK);
3038 
3039  const char *corrupted_header_name =
3040  "GET / HTTP/1.1\r\n"
3041  "Host: www.example.com\r\n"
3042  "X-Some-Header\r\033\065\325eep-Alive\r\n"
3043  "Accept-Encoding: gzip\r\n"
3044  "\r\n";
3045  test_simple(corrupted_header_name, HPE_INVALID_HEADER_TOKEN);
3046 
3047  const char *header_with_space =
3048  "GET / HTTP/1.1\r\n"
3049  "Host: www.example.com\r\n"
3050  "Foo Foo: some_value\r\n"
3051  "Accept-Encoding: gzip\r\n"
3052  "\r\n";
3053  test_simple(header_with_space, HPE_INVALID_HEADER_TOKEN);
3054 
3055  const char *header_with_curly_brace =
3056  "GET / HTTP/1.1\r\n"
3057  "Host: www.example.com\r\n"
3058  "Foo}Foo: some_value\r\n"
3059  "Accept-Encoding: gzip\r\n"
3060  "\r\n";
3061  test_simple(header_with_curly_brace, HPE_INVALID_HEADER_TOKEN);
3062 
3063  const char *header_with_quote =
3064  "GET / HTTP/1.1\r\n"
3065  "Host: www.example.com\r\n"
3066  "Foo\"Foo: some_value\r\n"
3067  "Accept-Encoding: gzip\r\n"
3068  "\r\n";
3069  test_simple(header_with_quote, HPE_INVALID_HEADER_TOKEN);
3070 
3071  const char *header_with_forward_slash =
3072  "GET / HTTP/1.1\r\n"
3073  "Host: www.example.com\r\n"
3074  "Foo/Foo: some_value\r\n"
3075  "Accept-Encoding: gzip\r\n"
3076  "\r\n";
3077  test_simple(header_with_forward_slash, HPE_INVALID_HEADER_TOKEN);
3078 
3079  const char *header_with_trailing_space =
3080  "GET / HTTP/1.1\r\n"
3081  "Host: www.example.com\r\n"
3082  "X-Some-Header : some_value\r\n"
3083  "\r\n";
3084  test_simple(header_with_trailing_space, HPE_INVALID_HEADER_TOKEN);
3085 
3086  const char *bad_end_of_headers_1 =
3087  "GET / HTTP/1.1\r\n"
3088  "Host: www.example1.com\r\n"
3089  "X-Some-Header: some_value"
3090  "\r\n\r*";
3091  test_simple(bad_end_of_headers_1, HPE_STRICT);
3092 
3093  const char *bad_end_of_headers_2 =
3094  "GET / HTTP/1.1\r\n"
3095  "Host: www.example2.com\r\n"
3096  "X-Some-Header: some_value"
3097  "\n\r*";
3098  test_simple(bad_end_of_headers_2, HPE_STRICT);
3099 
3100  const char *empty_content_length_header =
3101  "GET / HTTP/1.1\r\n"
3102  "Host: www.example.com\r\n"
3103  "Content-Length:\r\n"
3104  "Accept-Encoding: gzip\r\n"
3105  "\r\n";
3106  test_simple(empty_content_length_header, HPE_INVALID_CONTENT_LENGTH);
3107 
3108  const char *empty_transfer_encoding_header =
3109  "GET / HTTP/1.1\r\n"
3110  "Host: www.example.com\r\n"
3111  "Transfer-Encoding:\r\n"
3112  "Accept-Encoding: gzip\r\n"
3113  "\r\n";
3114  test_simple(empty_transfer_encoding_header, HPE_INVALID_TRANSFER_ENCODING);
3115 
3116  const char *empty_upgrade_header =
3117  "GET / HTTP/1.1\r\n"
3118  "Host: www.example.com\r\n"
3119  "Upgrade:\r\n"
3120  "Accept-Encoding: gzip\r\n"
3121  "\r\n";
3122  test_simple(empty_upgrade_header, HPE_INVALID_UPGRADE);
3123 
3124  #if 0
3125  // NOTE(Wed Nov 18 11:57:27 CET 2009) this seems okay. we just read body
3126  // until EOF.
3127  //
3128  // no content-length
3129  // error if there is a body without content length
3130  const char *bad_get_no_headers_no_body = "GET /bad_get_no_headers_no_body/world HTTP/1.1\r\n"
3131  "Accept: */*\r\n"
3132  "\r\n"
3133  "HELLO";
3134  test_simple(bad_get_no_headers_no_body, 0);
3135 #endif
3136  /* TODO sending junk and large headers gets rejected */
3137 
3138 
3139  /* check to make sure our predefined requests are okay */
3140  for (i = 0; requests[i].name; i++) {
3141  test_message(&requests[i]);
3142  }
3143 
3144  for (i = 0; i < request_count; i++) {
3146  }
3147 
3148  for (i = 0; i < request_count; i++) {
3149  if (!requests[i].should_keep_alive) continue;
3150  for (j = 0; j < request_count; j++) {
3151  if (!requests[j].should_keep_alive) continue;
3152  for (k = 0; k < request_count; k++) {
3153  test_multiple3(&requests[i], &requests[j], &requests[k]);
3154  }
3155  }
3156  }
3157 
3158  printf("request scan 1/4 ");
3161  , &requests[GET_NO_HEADERS_NO_BODY]
3162  );
3163 
3164  printf("request scan 2/4 ");
3168  );
3169 
3170  printf("request scan 3/4 ");
3174  );
3175 
3176  printf("request scan 4/4 ");
3180  );
3181 
3182  puts("requests okay");
3183 
3184  return 0;
3185 }
void test_multiple3(const struct message *r1, const struct message *r2, const struct message *r3)
Definition: test.c:2565
#define GET_FUNKY_CONTENT_LENGTH
Definition: test.c:42
int chunk_lengths[16]
Definition: test.c:60
const struct message requests[]
Definition: test.c:80
const struct message responses[]
Definition: test.c:791
#define MAX_CHUNKS
Definition: test.c:36
void test_simple(const char *buf, enum http_errno err_expected)
Definition: test.c:2473
#define QUERY_URL_WITH_QUESTION_MARK_GET
#define GET_ONE_HEADER_NO_BODY
#define NO_CARRIAGE_RET
void test_message(const struct message *message)
Definition: test.c:2377
void test_on_message_begin_cb(const struct message *message)
Definition: test.c:2824
void test_scan(const struct message *r1, const struct message *r2, const struct message *r3)
Definition: test.c:2622
static http_parser * parser
Definition: test.c:40
void test_message_count_body(const struct message *message)
Definition: test.c:2437
#define TRAILING_SPACE_ON_CHUNKED_BODY
char * create_large_chunked_message(int body_size_in_kb, const char *headers)
Definition: test.c:2740
void test_parse_url(void)
Definition: test.c:2330
#define CONNECT_REQUEST
void test_message_pause(const struct message *msg)
Definition: test.c:2771
#define BONJOUR_MADAME_FR
#define CHUNKED_W_TRAILING_HEADERS
#define TWO_CHUNKS_MULT_ZERO_END
void free()
#define FALSE
Definition: test.c:32
#define POST_IDENTITY_BODY_WORLD
#define CHUNKED_W_BULLSHIT_AFTER_LENGTH
#define GET_NO_HEADERS_NO_BODY
int should_keep_alive
Definition: test.c:56
#define NO_HEADERS_NO_BODY_404
const struct message on_message_begin_cb_test[]
Definition: test.c:1138
#define NO_REASON_PHRASE
void test_header_overflow_error(int req)
Definition: test.c:2504
KeyT k
#define UNDERSTORE_HEADER_KEY
#define PREFIX_NEWLINE_GET
#define POST_CHUNKED_ALL_YOUR_BASE
void test_no_overflow_long_body(int req, size_t length)
Definition: test.c:2532
const char * name
Definition: test.c:43
int message_begin_cb ( http_parser p)

Definition at line 1265 of file test.c.

References message::message_begin_cb_called, messages, and num_messages.

Referenced by pause_message_begin_cb().

1266 {
1267  assert(p == parser);
1269  return 0;
1270 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
static http_parser * parser
Definition: test.c:40
int message_begin_cb_called
Definition: test.c:67
int message_complete_cb ( http_parser p)

Definition at line 1286 of file test.c.

References currently_parsing_eof, message::message_complete_cb_called, message::message_complete_on_eof, messages, num_messages, and TRUE.

Referenced by pause_message_complete_cb().

1287 {
1288  assert(p == parser);
1290 
1292 
1293  num_messages++;
1294  return 0;
1295 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
int message_complete_cb_called
Definition: test.c:69
static http_parser * parser
Definition: test.c:40
#define TRUE
Definition: test.c:30
int message_complete_on_eof
Definition: test.c:70
static int currently_parsing_eof
Definition: test.c:73
int message_eq ( int  index,
const struct message expected 
)

Definition at line 1660 of file test.c.

References message::body, message::body_size, check_str_eq(), message::chunk_lengths, message::headers, message::headers_complete_cb_called, message::http_major, message::http_minor, HTTP_REQUEST, i, m, MAX_CHUNKS, message::message_begin_cb_called, MESSAGE_CHECK_NUM_EQ, MESSAGE_CHECK_STR_EQ, message::message_complete_cb_called, message::message_complete_on_eof, messages, message::method, message::num_chunks, message::num_chunks_complete, message::num_headers, message::request_url, message::response_reason, message::status_code, message::type, and message::upgrade.

Referenced by test_message(), test_message_count_body(), test_message_pause(), test_multiple3(), and test_scan().

1661 {
1662  int i;
1663  struct message *m = &messages[index];
1664 
1665  MESSAGE_CHECK_NUM_EQ(expected, m, http_major);
1666  MESSAGE_CHECK_NUM_EQ(expected, m, http_minor);
1667 
1668  if (expected->type == HTTP_REQUEST) {
1669  MESSAGE_CHECK_NUM_EQ(expected, m, method);
1670  } else {
1671  MESSAGE_CHECK_NUM_EQ(expected, m, status_code);
1672  MESSAGE_CHECK_STR_EQ(expected, m, response_reason);
1673  }
1674 
1676 
1677  assert(m->message_begin_cb_called);
1678  assert(m->headers_complete_cb_called);
1679  assert(m->message_complete_cb_called);
1680 
1681 
1682  MESSAGE_CHECK_STR_EQ(expected, m, request_url);
1683  if (expected->body_size) {
1684  MESSAGE_CHECK_NUM_EQ(expected, m, body_size);
1685  } else {
1686  MESSAGE_CHECK_STR_EQ(expected, m, body);
1687  }
1688 
1689  MESSAGE_CHECK_NUM_EQ(expected, m, num_chunks);
1691  for (i = 0; i < m->num_chunks && i < MAX_CHUNKS; i++) {
1692  MESSAGE_CHECK_NUM_EQ(expected, m, chunk_lengths[i]);
1693  }
1694 
1695  MESSAGE_CHECK_NUM_EQ(expected, m, num_headers);
1696 
1697  int r;
1698  for (i = 0; i < m->num_headers; i++) {
1699  r = check_str_eq(expected, "header field", expected->headers[i][0], m->headers[i][0]);
1700  if (!r) return 0;
1701  r = check_str_eq(expected, "header value", expected->headers[i][1], m->headers[i][1]);
1702  if (!r) return 0;
1703  }
1704 
1705  MESSAGE_CHECK_STR_EQ(expected, m, upgrade);
1706 
1707  return 1;
1708 }
static struct message messages[5]
Definition: test.c:75
Definition: test.c:42
int chunk_lengths[16]
Definition: test.c:60
enum http_method method
Definition: test.c:46
#define MAX_CHUNKS
Definition: test.c:36
int headers_complete_cb_called
Definition: test.c:68
unsigned short http_major
Definition: test.c:64
#define MESSAGE_CHECK_NUM_EQ(expected, found, prop)
Definition: test.c:1655
int message_complete_cb_called
Definition: test.c:69
const char * upgrade
Definition: test.c:62
enum http_parser_type type
Definition: test.c:45
int num_chunks
Definition: test.c:58
int message_complete_on_eof
Definition: test.c:70
int status_code
Definition: test.c:47
int num_chunks_complete
Definition: test.c:59
static map< string, int > m
unsigned short http_minor
Definition: test.c:65
char headers[13][2][500]
Definition: test.c:55
int message_begin_cb_called
Definition: test.c:67
static int check_str_eq(const struct message *m, const char *prop, const char *expected, const char *found)
Definition: test.c:1619
size_t body_size
Definition: test.c:52
char body[500]
Definition: test.c:51
char response_reason[500]
Definition: test.c:49
char request_url[500]
Definition: test.c:48
int num_headers
Definition: test.c:53
#define MESSAGE_CHECK_STR_EQ(expected, found, prop)
Definition: test.c:1652
size_t parse ( const char *  buf,
size_t  len 
)

Definition at line 1591 of file test.c.

References currently_parsing_eof, and http_parser_execute().

Referenced by folly::DynamicParser::arrayItems(), HTTP2FramerTest::dataFrameTest(), folly::DynamicParser::objectItems(), folly::DynamicParser::optional(), HTTP2FramerTest::parse(), parseSPDY(), folly::DynamicParser::required(), folly::DynamicParser::setAllowNonStringKeyErrors(), TEST(), TEST_F(), test_message(), test_multiple3(), test_on_message_begin_cb(), test_scan(), test_simple(), and proxygen::RFC1867Base::testSimple().

1592 {
1593  size_t nparsed;
1594  currently_parsing_eof = (len == 0);
1595  nparsed = http_parser_execute(parser, &settings, buf, len);
1596  return nparsed;
1597 }
size_t http_parser_execute(http_parser *parser, const http_parser_settings *settings, const char *data, size_t len)
Definition: http_parser.c:602
static http_parser_settings settings
Definition: test.c:1529
static http_parser * parser
Definition: test.c:40
static int currently_parsing_eof
Definition: test.c:73
size_t parse_count_body ( const char *  buf,
size_t  len 
)

Definition at line 1599 of file test.c.

References currently_parsing_eof, and http_parser_execute().

Referenced by test_message_count_body().

1600 {
1601  size_t nparsed;
1602  currently_parsing_eof = (len == 0);
1603  nparsed = http_parser_execute(parser, &settings_count_body, buf, len);
1604  return nparsed;
1605 }
size_t http_parser_execute(http_parser *parser, const http_parser_settings *settings, const char *data, size_t len)
Definition: http_parser.c:602
static http_parser * parser
Definition: test.c:40
static int currently_parsing_eof
Definition: test.c:73
static http_parser_settings settings_count_body
Definition: test.c:1542
size_t parse_pause ( const char *  buf,
size_t  len 
)

Definition at line 1607 of file test.c.

References currently_parsing_eof, http_parser_execute(), s, and settings_pause.

Referenced by test_message_pause().

1608 {
1609  size_t nparsed;
1611 
1612  currently_parsing_eof = (len == 0);
1614  nparsed = http_parser_execute(parser, current_pause_parser, buf, len);
1615  return nparsed;
1616 }
size_t http_parser_execute(http_parser *parser, const http_parser_settings *settings, const char *data, size_t len)
Definition: http_parser.c:602
static http_parser * parser
Definition: test.c:40
static int currently_parsing_eof
Definition: test.c:73
static http_parser_settings settings_pause
Definition: test.c:1516
static set< string > s
static http_parser_settings * current_pause_parser
Definition: test.c:77
void parser_free ( )

Definition at line 1584 of file test.c.

References bm::free().

Referenced by test_message(), test_message_count_body(), test_message_pause(), test_multiple3(), test_on_message_begin_cb(), test_scan(), and test_simple().

1585 {
1586  assert(parser);
1587  free(parser);
1588  parser = NULL;
1589 }
static http_parser * parser
Definition: test.c:40
void free()
void parser_init ( enum http_parser_type  type)

Definition at line 1569 of file test.c.

References http_parser_init(), messages, and num_messages.

Referenced by test_message(), test_message_count_body(), test_message_pause(), test_multiple3(), test_on_message_begin_cb(), test_scan(), and test_simple().

1570 {
1571  num_messages = 0;
1572 
1573  assert(parser == NULL);
1574 
1575  parser = malloc(sizeof(http_parser));
1576 
1578 
1579  memset(&messages, 0, sizeof messages);
1580 
1581 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
PskType type
static http_parser * parser
Definition: test.c:40
void http_parser_init(http_parser *parser, enum http_parser_type t)
Definition: http_parser.c:2166
int pause_body_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1466 of file test.c.

References body_cb(), http_parser_pause(), and settings_dontcall.

1467 {
1468  http_parser_pause(p, 1);
1470  return body_cb(p, buf, len);
1471 }
int body_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1246
void http_parser_pause(http_parser *parser, int paused)
Definition: http_parser.c:2431
static http_parser_settings * current_pause_parser
Definition: test.c:77
static http_parser_settings settings_dontcall
Definition: test.c:1416
int pause_chunk_complete_cb ( http_parser p)

Definition at line 1506 of file test.c.

References chunk_complete_cb(), http_parser_pause(), and settings_dontcall.

1507 {
1508  http_parser_pause(p, 1);
1510  return chunk_complete_cb(p);
1511 }
void http_parser_pause(http_parser *parser, int paused)
Definition: http_parser.c:2431
static http_parser_settings * current_pause_parser
Definition: test.c:77
static http_parser_settings settings_dontcall
Definition: test.c:1416
int chunk_complete_cb(http_parser *p)
Definition: test.c:1324
int pause_chunk_header_cb ( http_parser p)

Definition at line 1498 of file test.c.

References chunk_header_cb(), http_parser_pause(), and settings_dontcall.

1499 {
1500  http_parser_pause(p, 1);
1502  return chunk_header_cb(p);
1503 }
void http_parser_pause(http_parser *parser, int paused)
Definition: http_parser.c:2431
int chunk_header_cb(http_parser *p)
Definition: test.c:1306
static http_parser_settings * current_pause_parser
Definition: test.c:77
static http_parser_settings settings_dontcall
Definition: test.c:1416
int pause_header_field_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1442 of file test.c.

References header_field_cb(), http_parser_pause(), and settings_dontcall.

1443 {
1444  http_parser_pause(p, 1);
1446  return header_field_cb(p, buf, len);
1447 }
void http_parser_pause(http_parser *parser, int paused)
Definition: http_parser.c:2431
static http_parser_settings * current_pause_parser
Definition: test.c:77
static http_parser_settings settings_dontcall
Definition: test.c:1416
int header_field_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1212
int pause_header_value_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1450 of file test.c.

References header_value_cb(), http_parser_pause(), and settings_dontcall.

1451 {
1452  http_parser_pause(p, 1);
1454  return header_value_cb(p, buf, len);
1455 }
int header_value_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1228
void http_parser_pause(http_parser *parser, int paused)
Definition: http_parser.c:2431
static http_parser_settings * current_pause_parser
Definition: test.c:77
static http_parser_settings settings_dontcall
Definition: test.c:1416
int pause_headers_complete_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1474 of file test.c.

References headers_complete_cb(), http_parser_pause(), and settings_dontcall.

1475 {
1476  http_parser_pause(p, 1);
1478  return headers_complete_cb(p, buf, len);
1479 }
void http_parser_pause(http_parser *parser, int paused)
Definition: http_parser.c:2431
int headers_complete_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1273
static http_parser_settings * current_pause_parser
Definition: test.c:77
static http_parser_settings settings_dontcall
Definition: test.c:1416
int pause_message_begin_cb ( http_parser p)

Definition at line 1434 of file test.c.

References http_parser_pause(), message_begin_cb(), and settings_dontcall.

1435 {
1436  http_parser_pause(p, 1);
1438  return message_begin_cb(p);
1439 }
int message_begin_cb(http_parser *p)
Definition: test.c:1265
void http_parser_pause(http_parser *parser, int paused)
Definition: http_parser.c:2431
static http_parser_settings * current_pause_parser
Definition: test.c:77
static http_parser_settings settings_dontcall
Definition: test.c:1416
int pause_message_complete_cb ( http_parser p)

Definition at line 1482 of file test.c.

References http_parser_pause(), message_complete_cb(), and settings_dontcall.

1483 {
1484  http_parser_pause(p, 1);
1486  return message_complete_cb(p);
1487 }
void http_parser_pause(http_parser *parser, int paused)
Definition: http_parser.c:2431
int message_complete_cb(http_parser *p)
Definition: test.c:1286
static http_parser_settings * current_pause_parser
Definition: test.c:77
static http_parser_settings settings_dontcall
Definition: test.c:1416
int pause_request_url_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1458 of file test.c.

References http_parser_pause(), request_url_cb(), and settings_dontcall.

1459 {
1460  http_parser_pause(p, 1);
1462  return request_url_cb(p, buf, len);
1463 }
int request_url_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1204
void http_parser_pause(http_parser *parser, int paused)
Definition: http_parser.c:2431
static http_parser_settings * current_pause_parser
Definition: test.c:77
static http_parser_settings settings_dontcall
Definition: test.c:1416
int pause_response_reason_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1490 of file test.c.

References http_parser_pause(), response_reason_cb(), and settings_dontcall.

1491 {
1492  http_parser_pause(p, 1);
1494  return response_reason_cb(p, buf, len);
1495 }
void http_parser_pause(http_parser *parser, int paused)
Definition: http_parser.c:2431
int response_reason_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1298
static http_parser_settings * current_pause_parser
Definition: test.c:77
static http_parser_settings settings_dontcall
Definition: test.c:1416
static void print_error ( const char *  raw,
size_t  error_location 
)
static

Definition at line 1775 of file test.c.

References http_errno_description(), HTTP_PARSER_ERRNO, HTTP_PARSER_ERRNO_LINE, and i.

Referenced by test_message(), test_message_count_body(), test_multiple3(), and test_scan().

1776 {
1777  fprintf(stderr, "\n*** %s:%d -- %s ***\n\n",
1778  "http_parser.c", HTTP_PARSER_ERRNO_LINE(parser),
1780 
1781  int this_line = 0, char_len = 0;
1782  size_t i, j, len = strlen(raw), error_location_line = 0;
1783  for (i = 0; i < len; i++) {
1784  if (i == error_location) this_line = 1;
1785  switch (raw[i]) {
1786  case '\r':
1787  char_len = 2;
1788  fprintf(stderr, "\\r");
1789  break;
1790 
1791  case '\n':
1792  char_len = 2;
1793  fprintf(stderr, "\\n\n");
1794 
1795  if (this_line) goto print;
1796 
1797  error_location_line = 0;
1798  continue;
1799 
1800  default:
1801  char_len = 1;
1802  fputc(raw[i], stderr);
1803  break;
1804  }
1805  if (!this_line) error_location_line += char_len;
1806  }
1807 
1808  fprintf(stderr, "[eof]\n");
1809 
1810  print:
1811  for (j = 0; j < error_location_line; j++) {
1812  fputc(' ', stderr);
1813  }
1814  fprintf(stderr, "^\n\nerror location: %u\n", (unsigned int)error_location);
1815 }
#define HTTP_PARSER_ERRNO_LINE(p)
Definition: http_parser.h:208
static http_parser * parser
Definition: test.c:40
#define HTTP_PARSER_ERRNO(p)
Definition: http_parser.h:202
const char * http_errno_description(enum http_errno err)
Definition: http_parser.c:2186
const char * raw
Definition: test.c:44
int request_url_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1204 of file test.c.

References messages, num_messages, and message::request_url.

Referenced by pause_request_url_cb().

1205 {
1206  assert(p == parser);
1207  strncat(messages[num_messages].request_url, buf, len);
1208  return 0;
1209 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
static http_parser * parser
Definition: test.c:40
char request_url[500]
Definition: test.c:48
int response_reason_cb ( http_parser p,
const char *  buf,
size_t  len 
)

Definition at line 1298 of file test.c.

References messages, num_messages, and message::response_reason.

Referenced by pause_response_reason_cb().

1299 {
1300  assert(p == parser);
1301  strncat(messages[num_messages].response_reason, buf, len);
1302  return 0;
1303 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
static http_parser * parser
Definition: test.c:40
char response_reason[500]
Definition: test.c:49
void test_header_overflow_error ( int  req)

Definition at line 2504 of file test.c.

References HPE_HEADER_OVERFLOW, HTTP_PARSER_ERRNO, http_parser_execute(), http_parser_init(), HTTP_REQUEST, HTTP_RESPONSE, i, and parser.

Referenced by main().

2505 {
2507  http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
2508  size_t parsed;
2509  const char *buf;
2510  buf = req ? "GET / HTTP/1.1\r\n" : "HTTP/1.0 200 OK\r\n";
2511  parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf));
2512  assert(parsed == strlen(buf));
2513 
2514  buf = "header-key: header-value\r\n";
2515  size_t buflen = strlen(buf);
2516 
2517  int i;
2518  for (i = 0; i < 10000; i++) {
2519  parsed = http_parser_execute(&parser, &settings_null, buf, buflen);
2520  if (parsed != buflen) {
2521  //fprintf(stderr, "error found on iter %d\n", i);
2522  assert(HTTP_PARSER_ERRNO(&parser) == HPE_HEADER_OVERFLOW);
2523  return;
2524  }
2525  }
2526 
2527  fprintf(stderr, "\n*** Error expected but none in header overflow test ***\n");
2528  exit(1);
2529 }
static http_parser_settings settings_null
Definition: test.c:1555
size_t http_parser_execute(http_parser *parser, const http_parser_settings *settings, const char *data, size_t len)
Definition: http_parser.c:602
static http_parser * parser
Definition: test.c:40
#define HTTP_PARSER_ERRNO(p)
Definition: http_parser.h:202
void http_parser_init(http_parser *parser, enum http_parser_type t)
Definition: http_parser.c:2166
void test_message ( const struct message message)

Definition at line 2377 of file test.c.

References message_eq(), messages, message::name, num_messages, parse(), parser_free(), parser_init(), print_error(), message::raw, fizz::detail::read(), test(), message::type, message::upgrade, and http_parser::upgrade.

Referenced by main().

2378 {
2379  size_t raw_len = strlen(message->raw);
2380  size_t msg1len;
2381  for (msg1len = 0; msg1len < raw_len; msg1len++) {
2382  parser_init(message->type);
2383 
2384  size_t read;
2385  const char *msg1 = message->raw;
2386  const char *msg2 = msg1 + msg1len;
2387  size_t msg2len = raw_len - msg1len;
2388 
2389  if (msg1len) {
2390  read = parse(msg1, msg1len);
2391 
2392  if (message->upgrade && parser->upgrade && num_messages > 0) {
2393  messages[num_messages - 1].upgrade = msg1 + read;
2394  goto test;
2395  }
2396 
2397  if (read != msg1len) {
2398  print_error(msg1, read);
2399  exit(1);
2400  }
2401  }
2402 
2403 
2404  read = parse(msg2, msg2len);
2405 
2406  if (message->upgrade && parser->upgrade) {
2407  messages[num_messages - 1].upgrade = msg2 + read;
2408  goto test;
2409  }
2410 
2411  if (read != msg2len) {
2412  print_error(msg2, read);
2413  exit(1);
2414  }
2415 
2416  read = parse(NULL, 0);
2417 
2418  if (read != 0) {
2419  print_error(message->raw, read);
2420  exit(1);
2421  }
2422 
2423  test:
2424 
2425  if (num_messages != 1) {
2426  printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name);
2427  exit(1);
2428  }
2429 
2430  if(!message_eq(0, message)) exit(1);
2431 
2432  parser_free();
2433  }
2434 }
static int num_messages
Definition: test.c:76
size_t parse(const char *buf, size_t len)
Definition: test.c:1591
static struct message messages[5]
Definition: test.c:75
void parser_init(enum http_parser_type type)
Definition: test.c:1569
static uint64_t test(std::string name, bool fc_, bool dedicated_, bool tc_, bool syncops_, uint64_t base)
void parser_free()
Definition: test.c:1584
static http_parser * parser
Definition: test.c:40
const char * upgrade
Definition: test.c:62
enum http_parser_type type
Definition: test.c:45
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
static void print_error(const char *raw, size_t error_location)
Definition: test.c:1775
int message_eq(int index, const struct message *expected)
Definition: test.c:1660
const char * raw
Definition: test.c:44
const char * name
Definition: test.c:43
void test_message_count_body ( const struct message message)

Definition at line 2437 of file test.c.

References i, message_eq(), MIN, message::name, num_messages, parse_count_body(), parser_free(), parser_init(), print_error(), message::raw, fizz::detail::read(), and message::type.

Referenced by main().

2438 {
2439  parser_init(message->type);
2440 
2441  size_t read;
2442  size_t l = strlen(message->raw);
2443  size_t i, toread;
2444  size_t chunk = 4024;
2445 
2446  for (i = 0; i < l; i+= chunk) {
2447  toread = MIN(l-i, chunk);
2448  read = parse_count_body(message->raw + i, toread);
2449  if (read != toread) {
2450  print_error(message->raw, read);
2451  exit(1);
2452  }
2453  }
2454 
2455 
2456  read = parse_count_body(NULL, 0);
2457  if (read != 0) {
2458  print_error(message->raw, read);
2459  exit(1);
2460  }
2461 
2462  if (num_messages != 1) {
2463  printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name);
2464  exit(1);
2465  }
2466 
2467  if(!message_eq(0, message)) exit(1);
2468 
2469  parser_free();
2470 }
static int num_messages
Definition: test.c:76
void parser_init(enum http_parser_type type)
Definition: test.c:1569
void parser_free()
Definition: test.c:1584
enum http_parser_type type
Definition: test.c:45
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
static void print_error(const char *raw, size_t error_location)
Definition: test.c:1775
int message_eq(int index, const struct message *expected)
Definition: test.c:1660
const char * raw
Definition: test.c:44
#define MIN(a, b)
Definition: test.c:38
const char * name
Definition: test.c:43
size_t parse_count_body(const char *buf, size_t len)
Definition: test.c:1599
void test_message_pause ( const struct message msg)

Definition at line 2771 of file test.c.

References HPE_PAUSED, HPE_STRICT, HTTP_PARSER_ERRNO, http_parser_pause(), message::message_complete_cb_called, message_eq(), messages, message::name, num_messages, parse_pause(), parser_free(), parser_init(), message::raw, test(), message::type, message::upgrade, and http_parser::upgrade.

Referenced by main().

2772 {
2773  char *buf = (char*) msg->raw;
2774  size_t buflen = strlen(msg->raw);
2775  size_t nread;
2776 
2777  parser_init(msg->type);
2778 
2779  do {
2780  nread = parse_pause(buf, buflen);
2781 
2782  // We can only set the upgrade buffer once we've gotten our message
2783  // completion callback.
2785  msg->upgrade &&
2786  parser->upgrade) {
2787  messages[0].upgrade = buf + nread;
2788  goto test;
2789  }
2790 
2791  if (nread < buflen) {
2792 
2793  // Not much do to if we failed a strict-mode check
2795  parser_free();
2796  return;
2797  }
2798 
2799  assert (HTTP_PARSER_ERRNO(parser) == HPE_PAUSED);
2800  }
2801 
2802  buf += nread;
2803  buflen -= nread;
2805  } while (buflen > 0);
2806 
2807  nread = parse_pause(NULL, 0);
2808  assert (nread == 0);
2809 
2810 test:
2811  if (num_messages != 1) {
2812  printf("\n*** num_messages != 1 after testing '%s' ***\n\n", msg->name);
2813  exit(1);
2814  }
2815 
2816  if(!message_eq(0, msg)) exit(1);
2817 
2818  parser_free();
2819 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
void parser_init(enum http_parser_type type)
Definition: test.c:1569
size_t parse_pause(const char *buf, size_t len)
Definition: test.c:1607
static uint64_t test(std::string name, bool fc_, bool dedicated_, bool tc_, bool syncops_, uint64_t base)
void parser_free()
Definition: test.c:1584
int message_complete_cb_called
Definition: test.c:69
static http_parser * parser
Definition: test.c:40
const char * upgrade
Definition: test.c:62
enum http_parser_type type
Definition: test.c:45
void http_parser_pause(http_parser *parser, int paused)
Definition: http_parser.c:2431
#define HTTP_PARSER_ERRNO(p)
Definition: http_parser.h:202
int message_eq(int index, const struct message *expected)
Definition: test.c:1660
const char * raw
Definition: test.c:44
const char * name
Definition: test.c:43
void test_multiple3 ( const struct message r1,
const struct message r2,
const struct message r3 
)

Definition at line 2565 of file test.c.

References count_parsed_messages(), message_eq(), num_messages, parse(), parser_free(), parser_init(), print_error(), message::raw, fizz::detail::read(), test(), message::type, http_parser::upgrade, and upgrade_message_fix().

Referenced by main().

2566 {
2567  int message_count = count_parsed_messages(3, r1, r2, r3);
2568 
2569  char total[ strlen(r1->raw)
2570  + strlen(r2->raw)
2571  + strlen(r3->raw)
2572  + 1
2573  ];
2574  total[0] = '\0';
2575 
2576  strcat(total, r1->raw);
2577  strcat(total, r2->raw);
2578  strcat(total, r3->raw);
2579 
2580  parser_init(r1->type);
2581 
2582  size_t read;
2583 
2584  read = parse(total, strlen(total));
2585 
2586  if (parser->upgrade) {
2587  upgrade_message_fix(total, read, 3, r1, r2, r3);
2588  goto test;
2589  }
2590 
2591  if (read != strlen(total)) {
2592  print_error(total, read);
2593  exit(1);
2594  }
2595 
2596  read = parse(NULL, 0);
2597 
2598  if (read != 0) {
2599  print_error(total, read);
2600  exit(1);
2601  }
2602 
2603 test:
2604 
2605  if (message_count != num_messages) {
2606  fprintf(stderr, "\n\n*** Parser didn't see 3 messages only %d *** \n", num_messages);
2607  exit(1);
2608  }
2609 
2610  if (!message_eq(0, r1)) exit(1);
2611  if (message_count > 1 && !message_eq(1, r2)) exit(1);
2612  if (message_count > 2 && !message_eq(2, r3)) exit(1);
2613 
2614  parser_free();
2615 }
static int num_messages
Definition: test.c:76
size_t parse(const char *buf, size_t len)
Definition: test.c:1591
size_t count_parsed_messages(const size_t nmsgs,...)
Definition: test.c:1715
void parser_init(enum http_parser_type type)
Definition: test.c:1569
static uint64_t test(std::string name, bool fc_, bool dedicated_, bool tc_, bool syncops_, uint64_t base)
void parser_free()
Definition: test.c:1584
static http_parser * parser
Definition: test.c:40
enum http_parser_type type
Definition: test.c:45
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
static void print_error(const char *raw, size_t error_location)
Definition: test.c:1775
int message_eq(int index, const struct message *expected)
Definition: test.c:1660
void upgrade_message_fix(char *body, const size_t nread, const size_t nmsgs,...)
Definition: test.c:1738
const char * raw
Definition: test.c:44
void test_no_overflow_long_body ( int  req,
size_t  length 
)

Definition at line 2532 of file test.c.

References http_parser_execute(), http_parser_init(), HTTP_REQUEST, HTTP_RESPONSE, i, and parser.

Referenced by main().

2533 {
2535  http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE);
2536  size_t parsed;
2537  size_t i;
2538  char buf1[3000];
2539  size_t buf1len = sprintf(buf1, "%s\r\nConnection: Keep-Alive\r\nContent-Length: %zu\r\n\r\n",
2540  req ? "POST / HTTP/1.0" : "HTTP/1.0 200 OK", length);
2541  parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len);
2542  if (parsed != buf1len)
2543  goto err;
2544 
2545  for (i = 0; i < length; i++) {
2546  char foo = 'a';
2547  parsed = http_parser_execute(&parser, &settings_null, &foo, 1);
2548  if (parsed != 1)
2549  goto err;
2550  }
2551 
2552  parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len);
2553  if (parsed != buf1len) goto err;
2554  return;
2555 
2556  err:
2557  fprintf(stderr,
2558  "\n*** error in test_no_overflow_long_body %s of length %zu ***\n",
2559  req ? "REQUEST" : "RESPONSE",
2560  length);
2561  exit(1);
2562 }
static http_parser_settings settings_null
Definition: test.c:1555
size_t http_parser_execute(http_parser *parser, const http_parser_settings *settings, const char *data, size_t len)
Definition: http_parser.c:602
static http_parser * parser
Definition: test.c:40
void http_parser_init(http_parser *parser, enum http_parser_type t)
Definition: http_parser.c:2166
void test_on_message_begin_cb ( const struct message message)

Definition at line 2824 of file test.c.

References message::message_begin_cb_called, messages, num_messages, parse(), parser_free(), parser_init(), message::raw, and message::type.

Referenced by main().

2825 {
2826  parser_init(message->type);
2828  parse(message->raw, strlen(message->raw));
2830  parser_free();
2831 }
static int num_messages
Definition: test.c:76
size_t parse(const char *buf, size_t len)
Definition: test.c:1591
static struct message messages[5]
Definition: test.c:75
void parser_init(enum http_parser_type type)
Definition: test.c:1569
void parser_free()
Definition: test.c:1584
enum http_parser_type type
Definition: test.c:45
int message_begin_cb_called
Definition: test.c:67
const char * raw
Definition: test.c:44
void test_parse_url ( void  )

Definition at line 2330 of file test.c.

References dump_url(), http_parser_parse_url(), i, url_test::is_connect, url_test::name, url_test::rv, test(), url_test::u, url_test::url, and url_tests.

Referenced by main().

2331 {
2332  struct http_parser_url u;
2333  const struct url_test *test;
2334  unsigned int i;
2335  int rv;
2336 
2337  for (i = 0; i < (sizeof(url_tests) / sizeof(url_tests[0])); i++) {
2338  test = &url_tests[i];
2339  memset(&u, 0, sizeof(u));
2340 
2341  rv = http_parser_parse_url(test->url,
2342  strlen(test->url),
2343  test->is_connect,
2344  &u);
2345 
2346  if (test->rv == 0) {
2347  if (rv != 0) {
2348  printf("\n*** http_parser_parse_url(\"%s\") \"%s\" test failed, "
2349  "unexpected rv %d ***\n\n", test->url, test->name, rv);
2350  abort();
2351  }
2352 
2353  if (memcmp(&u, &test->u, sizeof(u)) != 0) {
2354  printf("\n*** http_parser_parse_url(\"%s\") \"%s\" failed ***\n",
2355  test->url, test->name);
2356 
2357  printf("target http_parser_url:\n");
2358  dump_url(test->url, &test->u);
2359  printf("result http_parser_url:\n");
2360  dump_url(test->url, &u);
2361 
2362  abort();
2363  }
2364  } else {
2365  /* test->rv != 0 */
2366  if (rv == 0) {
2367  printf("\n*** http_parser_parse_url(\"%s\") \"%s\" test failed, "
2368  "unexpected rv %d ***\n\n", test->url, test->name, rv);
2369  abort();
2370  }
2371  }
2372  }
2373 }
static uint64_t test(std::string name, bool fc_, bool dedicated_, bool tc_, bool syncops_, uint64_t base)
const char * url
Definition: test.c:1820
int rv
Definition: test.c:1823
void dump_url(const char *url, const struct http_parser_url *u)
Definition: test.c:2309
int http_parser_parse_url(const char *buf, size_t buflen, int is_connect, struct http_parser_url *u)
Definition: http_parser.c:2332
struct http_parser_url u
Definition: test.c:1822
const struct url_test url_tests[]
Definition: test.c:1826
const char * name
Definition: test.c:1819
int is_connect
Definition: test.c:1821
void test_scan ( const struct message r1,
const struct message r2,
const struct message r3 
)

Definition at line 2622 of file test.c.

References count_parsed_messages(), folly::pushmi::operators::error(), HTTP_BOTH, i, message_eq(), num_messages, ops, parse(), parser_free(), parser_init(), print_error(), message::raw, fizz::detail::read(), test(), message::type, http_parser::upgrade, and upgrade_message_fix().

Referenced by main().

2623 {
2624  char total[80*1024] = "\0";
2625  char buf1[80*1024] = "\0";
2626  char buf2[80*1024] = "\0";
2627  char buf3[80*1024] = "\0";
2628 
2629  strcat(total, r1->raw);
2630  strcat(total, r2->raw);
2631  strcat(total, r3->raw);
2632 
2633  size_t read;
2634 
2635  int total_len = strlen(total);
2636 
2637  int total_ops = 2 * (total_len - 1) * (total_len - 2) / 2;
2638  int ops = 0 ;
2639 
2640  size_t buf1_len, buf2_len, buf3_len;
2641  int message_count = count_parsed_messages(3, r1, r2, r3);
2642 
2643  int i,j,type_both;
2644  for (type_both = 0; type_both < 2; type_both ++ ) {
2645  for (j = 2; j < total_len; j ++ ) {
2646  for (i = 1; i < j; i ++ ) {
2647 
2648  if (ops % 1000 == 0) {
2649  printf("\b\b\b\b%3.0f%%", 100 * (float)ops /(float)total_ops);
2650  fflush(stdout);
2651  }
2652  ops += 1;
2653 
2654  parser_init(type_both ? HTTP_BOTH : r1->type);
2655 
2656  buf1_len = i;
2657  strncpy(buf1, total, buf1_len);
2658  buf1[buf1_len] = 0;
2659 
2660  buf2_len = j - i;
2661  strncpy(buf2, total+i, buf2_len);
2662  buf2[buf2_len] = 0;
2663 
2664  buf3_len = total_len - j;
2665  strncpy(buf3, total+j, buf3_len);
2666  buf3[buf3_len] = 0;
2667 
2668  read = parse(buf1, buf1_len);
2669 
2670  if (parser->upgrade) goto test;
2671 
2672  if (read != buf1_len) {
2673  print_error(buf1, read);
2674  goto error;
2675  }
2676 
2677  read += parse(buf2, buf2_len);
2678 
2679  if (parser->upgrade) goto test;
2680 
2681  if (read != buf1_len + buf2_len) {
2682  print_error(buf2, read);
2683  goto error;
2684  }
2685 
2686  read += parse(buf3, buf3_len);
2687  if (parser->upgrade) goto test;
2688 
2689  if (read != buf1_len + buf2_len + buf3_len) {
2690  print_error(buf3, read);
2691  goto error;
2692  }
2693 
2694  parse(NULL, 0);
2695 
2696 test:
2697  if (parser->upgrade) {
2698  upgrade_message_fix(total, read, 3, r1, r2, r3);
2699  }
2700 
2701  if (message_count != num_messages) {
2702  fprintf(stderr, "\n\nParser didn't see %d messages only %d\n",
2703  message_count, num_messages);
2704  goto error;
2705  }
2706 
2707  if (!message_eq(0, r1)) {
2708  fprintf(stderr, "\n\nError matching messages[0] in test_scan.\n");
2709  goto error;
2710  }
2711 
2712  if (message_count > 1 && !message_eq(1, r2)) {
2713  fprintf(stderr, "\n\nError matching messages[1] in test_scan.\n");
2714  goto error;
2715  }
2716 
2717  if (message_count > 2 && !message_eq(2, r3)) {
2718  fprintf(stderr, "\n\nError matching messages[2] in test_scan.\n");
2719  goto error;
2720  }
2721 
2722  parser_free();
2723  }
2724  }
2725  }
2726  puts("\b\b\b\b100%");
2727  return;
2728 
2729  error:
2730  fprintf(stderr, "i=%d j=%d\n", i, j);
2731  fprintf(stderr, "buf1 (%u) %s\n\n", (unsigned int)buf1_len, buf1);
2732  fprintf(stderr, "buf2 (%u) %s\n\n", (unsigned int)buf2_len , buf2);
2733  fprintf(stderr, "buf3 (%u) %s\n", (unsigned int)buf3_len, buf3);
2734  exit(1);
2735 }
static int num_messages
Definition: test.c:76
size_t parse(const char *buf, size_t len)
Definition: test.c:1591
size_t count_parsed_messages(const size_t nmsgs,...)
Definition: test.c:1715
void parser_init(enum http_parser_type type)
Definition: test.c:1569
static uint64_t test(std::string name, bool fc_, bool dedicated_, bool tc_, bool syncops_, uint64_t base)
void parser_free()
Definition: test.c:1584
static http_parser * parser
Definition: test.c:40
requires And< SemiMovable< VN >... > &&SemiMovable< E > auto error(E e)
Definition: error.h:48
enum http_parser_type type
Definition: test.c:45
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
static void print_error(const char *raw, size_t error_location)
Definition: test.c:1775
const int ops
int message_eq(int index, const struct message *expected)
Definition: test.c:1660
void upgrade_message_fix(char *body, const size_t nread, const size_t nmsgs,...)
Definition: test.c:1738
const char * raw
Definition: test.c:44
void test_simple ( const char *  buf,
enum http_errno  err_expected 
)

Definition at line 2473 of file test.c.

References HPE_OK, HPE_STRICT, http_errno_name(), HTTP_PARSER_ERRNO, HTTP_REQUEST, parse(), parser_free(), and parser_init().

Referenced by main().

2474 {
2476 
2477  size_t parsed;
2478  int pass;
2479  enum http_errno err;
2480 
2481  parsed = parse(buf, strlen(buf));
2482  pass = (parsed == strlen(buf));
2483  err = HTTP_PARSER_ERRNO(parser);
2484  parsed = parse(NULL, 0);
2485  pass &= (parsed == 0);
2486 
2487  parser_free();
2488 
2489  /* In strict mode, allow us to pass with an unexpected HPE_STRICT as
2490  * long as the caller isn't expecting success.
2491  */
2492 #if HTTP_PARSER_STRICT
2493  if (err_expected != err && err_expected != HPE_OK && err != HPE_STRICT) {
2494 #else
2495  if (err_expected != err) {
2496 #endif
2497  fprintf(stderr, "\n*** test_simple expected %s, but saw %s ***\n\n%s\n",
2498  http_errno_name(err_expected), http_errno_name(err), buf);
2499  exit(1);
2500  }
2501 }
size_t parse(const char *buf, size_t len)
Definition: test.c:1591
const char * http_errno_name(enum http_errno err)
Definition: http_parser.c:2180
void parser_init(enum http_parser_type type)
Definition: test.c:1569
void parser_free()
Definition: test.c:1584
static http_parser * parser
Definition: test.c:40
#define HTTP_PARSER_ERRNO(p)
Definition: http_parser.h:202
http_errno
Definition: http_parser.h:196
void upgrade_message_fix ( char *  body,
const size_t  nread,
const size_t  nmsgs,
  ... 
)

Definition at line 1738 of file test.c.

References check_str_eq(), i, m, messages, num_messages, message::raw, and message::upgrade.

Referenced by test_multiple3(), and test_scan().

1738  {
1739  va_list ap;
1740  size_t i;
1741  size_t off = 0;
1742 
1743  va_start(ap, nmsgs);
1744 
1745  for (i = 0; i < nmsgs; i++) {
1746  struct message *m = va_arg(ap, struct message *);
1747 
1748  off += strlen(m->raw);
1749 
1750  if (m->upgrade) {
1751  off -= strlen(m->upgrade);
1752 
1753  /* Check the portion of the response after its specified upgrade */
1754  if (!check_str_eq(m, "upgrade", body + off, body + nread)) {
1755  exit(1);
1756  }
1757 
1758  /* Fix up the response so that message_eq() will verify the beginning
1759  * of the upgrade */
1760  *(body + nread + strlen(m->upgrade)) = '\0';
1761  messages[num_messages -1 ].upgrade = body + nread;
1762 
1763  va_end(ap);
1764  return;
1765  }
1766  }
1767 
1768  va_end(ap);
1769  printf("\n\n*** Error: expected a message with upgrade ***\n");
1770 
1771  exit(1);
1772 }
static int num_messages
Definition: test.c:76
static struct message messages[5]
Definition: test.c:75
Definition: test.c:42
const char * upgrade
Definition: test.c:62
static map< string, int > m
static int check_str_eq(const struct message *m, const char *prop, const char *expected, const char *found)
Definition: test.c:1619
char body[500]
Definition: test.c:51
const char * raw
Definition: test.c:44

Variable Documentation

http_parser_settings* current_pause_parser
static

Definition at line 77 of file test.c.

int currently_parsing_eof
static

Definition at line 73 of file test.c.

Referenced by message_complete_cb(), parse(), parse_count_body(), and parse_pause().

const struct message on_message_begin_cb_test[]

Definition at line 1138 of file test.c.

http_parser* parser
static

Definition at line 40 of file test.c.

Referenced by folly::getGFlags(), test_header_overflow_error(), and test_no_overflow_long_body().

const struct message requests[]

Definition at line 80 of file test.c.

const struct message responses[]

Definition at line 791 of file test.c.

http_parser_settings settings
static
Initial value:
=
{.on_message_begin = message_begin_cb
,.on_header_field = header_field_cb
,.on_header_value = header_value_cb
,.on_url = request_url_cb
,.on_body = body_cb
,.on_headers_complete = headers_complete_cb
,.on_message_complete = message_complete_cb
,.on_reason = response_reason_cb
,.on_chunk_header = chunk_header_cb
,.on_chunk_complete = chunk_complete_cb
}
int request_url_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1204
int body_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1246
int header_value_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1228
int message_begin_cb(http_parser *p)
Definition: test.c:1265
int message_complete_cb(http_parser *p)
Definition: test.c:1286
int chunk_header_cb(http_parser *p)
Definition: test.c:1306
int headers_complete_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1273
int response_reason_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1298
int chunk_complete_cb(http_parser *p)
Definition: test.c:1324
int header_field_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1212

Definition at line 1529 of file test.c.

Referenced by proxygen::HTTPSessionBase::enableExHeadersSettings(), HTTPDownstreamTest< SPDY3_1CodecPair >::expectResponses(), proxygen::HTTP2Codec::generateSettings(), proxygen::HTTPSession::getCodecSendWindowSize(), proxygen::HTTP2Codec::getHeaderIndexingStrategy(), proxygen::HTTPSession::getSecurityProtocol(), proxygen::SPDYCodec::onControlFrame(), proxygen::HTTP2Codec::onIngressUpgradeMessage(), proxygen::HTTPSession::onNativeProtocolUpgradeImpl(), proxygen::HTTP2Codec::parseSettings(), proxygen::http2::parseSettings(), proxygen::PassThroughHTTPCodecFilter::PassThroughHTTPCodecFilter(), proxygen::HTTPSession::setEgressSettings(), proxygen::HTTPSession::setFlowControl(), proxygen::HTTPSession::setMaxConcurrentIncomingStreams(), proxygen::HTTPSession::setupCodec(), proxygen::HTTPDownstreamSession::setupOnHeadersComplete(), TEST(), TEST_F(), HTTP2CodecTest::testFrameSizeLimit(), HTTP2CodecTest::testHeaderListSize(), and TYPED_TEST_P().

http_parser_settings settings_count_body
static
Initial value:
=
{.on_message_begin = message_begin_cb
,.on_header_field = header_field_cb
,.on_header_value = header_value_cb
,.on_url = request_url_cb
,.on_body = count_body_cb
,.on_headers_complete = headers_complete_cb
,.on_message_complete = message_complete_cb
,.on_reason = response_reason_cb
,.on_chunk_header = chunk_header_cb
,.on_chunk_complete = chunk_complete_cb
}
int request_url_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1204
int count_body_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1256
int header_value_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1228
int message_begin_cb(http_parser *p)
Definition: test.c:1265
int message_complete_cb(http_parser *p)
Definition: test.c:1286
int chunk_header_cb(http_parser *p)
Definition: test.c:1306
int headers_complete_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1273
int response_reason_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1298
int chunk_complete_cb(http_parser *p)
Definition: test.c:1324
int header_field_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1212

Definition at line 1542 of file test.c.

http_parser_settings settings_dontcall
static
Initial value:
=
{.on_message_begin = dontcall_message_begin_cb
,.on_header_field = dontcall_header_field_cb
,.on_header_value = dontcall_header_value_cb
,.on_body = dontcall_body_cb
,.on_headers_complete = dontcall_headers_complete_cb
,.on_message_complete = dontcall_message_complete_cb
,.on_chunk_header = dontcall_chunk_header_cb
,.on_chunk_complete = dontcall_chunk_complete_cb
}
int dontcall_response_reason_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1392
int dontcall_chunk_header_cb(http_parser *p)
Definition: test.c:1400
int dontcall_header_field_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1342
int dontcall_message_begin_cb(http_parser *p)
Definition: test.c:1334
int dontcall_headers_complete_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1374
int dontcall_request_url_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1358
int dontcall_chunk_complete_cb(http_parser *p)
Definition: test.c:1408
int dontcall_body_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1366
int dontcall_header_value_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1350
int dontcall_message_complete_cb(http_parser *p)
Definition: test.c:1383

Definition at line 1416 of file test.c.

Referenced by pause_body_cb(), pause_chunk_complete_cb(), pause_chunk_header_cb(), pause_header_field_cb(), pause_header_value_cb(), pause_headers_complete_cb(), pause_message_begin_cb(), pause_message_complete_cb(), pause_request_url_cb(), and pause_response_reason_cb().

http_parser_settings settings_null
static
Initial value:
=
{.on_message_begin = empty_cb
,.on_header_field = empty_data_cb
,.on_header_value = empty_data_cb
,.on_url = empty_data_cb
,.on_body = empty_data_cb
,.on_headers_complete = empty_data_cb
,.on_message_complete = empty_cb
,.on_reason = empty_data_cb
,.on_chunk_header = empty_cb
,.on_chunk_complete = empty_cb
}
int empty_data_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1514
int empty_cb(http_parser *p)
Definition: test.c:1513

Definition at line 1555 of file test.c.

http_parser_settings settings_pause
static
Initial value:
=
{.on_message_begin = pause_message_begin_cb
,.on_header_field = pause_header_field_cb
,.on_header_value = pause_header_value_cb
,.on_body = pause_body_cb
,.on_headers_complete = pause_headers_complete_cb
,.on_message_complete = pause_message_complete_cb
,.on_chunk_header = pause_chunk_header_cb
,.on_chunk_complete = pause_chunk_complete_cb
}
int pause_chunk_header_cb(http_parser *p)
Definition: test.c:1498
int pause_request_url_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1458
int pause_response_reason_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1490
int pause_message_complete_cb(http_parser *p)
Definition: test.c:1482
int pause_body_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1466
int pause_header_field_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1442
int pause_message_begin_cb(http_parser *p)
Definition: test.c:1434
int pause_header_value_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1450
int pause_headers_complete_cb(http_parser *p, const char *buf, size_t len)
Definition: test.c:1474
int pause_chunk_complete_cb(http_parser *p)
Definition: test.c:1506

Definition at line 1516 of file test.c.

Referenced by parse_pause().

const struct url_test url_tests[]

Definition at line 1826 of file test.c.

Referenced by test_parse_url().