GNU libmicrohttpd  0.9.29
proxy.c File Reference

Translates incoming SPDY requests to http server on localhost. Uses libcurl. No error handling for curl requests. TODO: More...

#include "platform.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include "microspdy.h"
#include <curl/curl.h>
#include <assert.h>
#include <getopt.h>
#include <regex.h>
Include dependency graph for proxy.c:

Go to the source code of this file.

Macros

#define ERROR_RESPONSE   "502 Bad Gateway"
 
#define PRINT_INFO(msg)
 
#define PRINT_INFO2(fmt, ...)
 
#define PRINT_VERBOSE(msg)
 
#define PRINT_VERBOSE2(fmt, ...)
 
#define CURL_SETOPT(handle, opt, val)
 
#define DIE(msg)
 

Functions

static void free_uri (struct URI *uri)
 
static int init_parse_uri (regex_t *preg)
 
static void deinit_parse_uri (regex_t *preg)
 
static int parse_uri (regex_t *preg, const char *full_uri, struct URI **uri)
 
static bool store_in_buffer (const void *src, size_t src_size, void **dst, size_t *dst_size)
 
static ssize_t get_from_buffer (void **src, size_t *src_size, void *dst, size_t max_size)
 
static void catch_signal (int signal)
 
static void new_session_cb (void *cls, struct SPDY_Session *session)
 
static void session_closed_cb (void *cls, struct SPDY_Session *session, int by_client)
 
static int spdy_post_data_cb (void *cls, struct SPDY_Request *request, const void *buf, size_t size, bool more)
 
ssize_t response_callback (void *cls, void *buffer, size_t max, bool *more)
 
static void cleanup (struct Proxy *proxy)
 
static void response_done_callback (void *cls, struct SPDY_Response *response, struct SPDY_Request *request, enum SPDY_RESPONSE_RESULT status, bool streamopened)
 
static size_t curl_header_cb (void *ptr, size_t size, size_t nmemb, void *userp)
 
static size_t curl_write_cb (void *contents, size_t size, size_t nmemb, void *userp)
 
static size_t curl_read_cb (void *ptr, size_t size, size_t nmemb, void *userp)
 
static int iterate_cb (void *cls, const char *name, const char *const *value, int num_values)
 
static void standard_request_handler (void *cls, struct SPDY_Request *request, uint8_t priority, const char *method, const char *path, const char *version, const char *host, const char *scheme, struct SPDY_NameValue *headers, bool more)
 
static int run ()
 
static void display_usage ()
 
int main (int argc, char *const *argv)
 

Variables

struct global_options glob_opt
 
static int loop = 1
 
static CURLM * multi_handle
 
static int still_running = 0
 
static regex_t uri_preg
 
static bool call_spdy_run
 
static bool call_curl_run
 
int debug_num_curls
 

Detailed Description

Translates incoming SPDY requests to http server on localhost. Uses libcurl. No error handling for curl requests. TODO:

  • test all options!
  • don't abort on lack of memory
  • Correct recapitalizetion of header names before giving the headers to curl.
  • curl does not close sockets when connection is closed and no new sockets are opened (they stay in CLOSE_WAIT)
  • add '/' when a user requests http://example.com . Now this is a bad request
  • curl returns 0 or 1 ms for timeout even when nothing will be done; thus the loop uses CPU for nothing
    Author
    Andrey Uzunov

Definition in file proxy.c.

Macro Definition Documentation

#define CURL_SETOPT (   handle,
  opt,
  val 
)
Value:
do{\
int ret; \
if(CURLE_OK != (ret = curl_easy_setopt(handle, opt, val))) \
{ \
PRINT_INFO2("curl_easy_setopt failed (%i = %i)", opt, ret); \
abort(); \
} \
}\
while(0)
#define PRINT_INFO2(fmt,...)
Definition: proxy.c:97

Definition at line 126 of file proxy.c.

Referenced by standard_request_handler().

#define DIE (   msg)
Value:
do{\
printf("FATAL ERROR (line %i): %s\n", __LINE__, msg);\
fflush(stdout);\
exit(EXIT_FAILURE);\
}\
while(0)

Definition at line 137 of file proxy.c.

Referenced by cleanup(), curl_header_cb(), iterate_cb(), main(), new_session_cb(), run(), and standard_request_handler().

#define ERROR_RESPONSE   "502 Bad Gateway"

Definition at line 53 of file proxy.c.

Referenced by run().

#define PRINT_INFO (   msg)
Value:
do{\
fprintf(stdout, "%i:%s\n", __LINE__, msg);\
fflush(stdout);\
}\
while(0)

Definition at line 90 of file proxy.c.

Referenced by curl_read_cb(), curl_write_cb(), main(), response_callback(), run(), spdy_post_data_cb(), and standard_request_handler().

#define PRINT_INFO2 (   fmt,
  ... 
)
Value:
do{\
fprintf(stdout, "%i\n", __LINE__);\
fprintf(stdout, fmt,##__VA_ARGS__);\
fprintf(stdout, "\n");\
fflush(stdout);\
}\
while(0)

Definition at line 97 of file proxy.c.

Referenced by cleanup(), curl_header_cb(), run(), spdy_post_data_cb(), and standard_request_handler().

#define PRINT_VERBOSE (   msg)
Value:
do{\
if(glob_opt.verbose){\
fprintf(stdout, "%i:%s\n", __LINE__, msg);\
fflush(stdout);\
}\
}\
while(0)
struct global_options glob_opt

Definition at line 106 of file proxy.c.

Referenced by curl_header_cb(), curl_read_cb(), curl_write_cb(), new_session_cb(), response_callback(), run(), and spdy_post_data_cb().

#define PRINT_VERBOSE2 (   fmt,
  ... 
)
Value:
do{\
if(glob_opt.verbose){\
fprintf(stdout, "%i\n", __LINE__);\
fprintf(stdout, fmt,##__VA_ARGS__);\
fprintf(stdout, "\n");\
fflush(stdout);\
}\
}\
while(0)
struct global_options glob_opt

Definition at line 115 of file proxy.c.

Referenced by curl_header_cb(), curl_read_cb(), curl_write_cb(), iterate_cb(), response_callback(), run(), session_closed_cb(), spdy_post_data_cb(), and standard_request_handler().

Function Documentation

static void catch_signal ( int  signal)
static

Definition at line 359 of file proxy.c.

References loop.

Referenced by run().

Here is the caller graph for this function:

static void cleanup ( struct Proxy *  proxy)
static

Definition at line 483 of file proxy.c.

References debug_num_curls, DIE, multi_handle, and PRINT_INFO2.

Referenced by internal_add_connection(), response_done_callback(), and run().

Here is the caller graph for this function:

static size_t curl_header_cb ( void *  ptr,
size_t  size,
size_t  nmemb,
void *  userp 
)
static
static size_t curl_read_cb ( void *  ptr,
size_t  size,
size_t  nmemb,
void *  userp 
)
static

Definition at line 727 of file proxy.c.

References get_from_buffer(), PRINT_INFO, PRINT_VERBOSE, and PRINT_VERBOSE2.

Referenced by standard_request_handler().

Here is the call graph for this function:

Here is the caller graph for this function:

static size_t curl_write_cb ( void *  contents,
size_t  size,
size_t  nmemb,
void *  userp 
)
static

Definition at line 683 of file proxy.c.

References call_spdy_run, PRINT_INFO, PRINT_VERBOSE, PRINT_VERBOSE2, and store_in_buffer().

Referenced by standard_request_handler().

Here is the call graph for this function:

Here is the caller graph for this function:

static void deinit_parse_uri ( regex_t *  preg)
static

Definition at line 226 of file proxy.c.

Referenced by run().

Here is the caller graph for this function:

static void display_usage ( )
static

Definition at line 1260 of file proxy.c.

Referenced by main().

Here is the caller graph for this function:

static void free_uri ( struct URI *  uri)
static

Definition at line 189 of file proxy.c.

References NULL.

Referenced by parse_uri(), and standard_request_handler().

Here is the caller graph for this function:

static ssize_t get_from_buffer ( void **  src,
size_t *  src_size,
void *  dst,
size_t  max_size 
)
static

Definition at line 332 of file proxy.c.

References NULL.

Referenced by curl_read_cb(), and response_callback().

Here is the caller graph for this function:

static int init_parse_uri ( regex_t *  preg)
static

Definition at line 209 of file proxy.c.

Referenced by run().

Here is the caller graph for this function:

static int iterate_cb ( void *  cls,
const char *  name,
const char *const *  value,
int  num_values 
)
static

Definition at line 790 of file proxy.c.

References DIE, NULL, PRINT_VERBOSE2, and SPDY_YES.

Referenced by standard_request_handler().

Here is the caller graph for this function:

int main ( int  argc,
char *const *  argv 
)

Definition at line 1296 of file proxy.c.

References DIE, display_usage(), glob_opt, NULL, PRINT_INFO, and run().

Here is the call graph for this function:

static void new_session_cb ( void *  cls,
struct SPDY_Session session 
)
static

Definition at line 367 of file proxy.c.

References DIE, NULL, PRINT_VERBOSE, and SPDY_set_cls_to_session().

Referenced by run().

Here is the call graph for this function:

Here is the caller graph for this function:

static int parse_uri ( regex_t *  preg,
const char *  full_uri,
struct URI **  uri 
)
static

Definition at line 233 of file proxy.c.

References free_uri(), and NULL.

Referenced by standard_request_handler().

Here is the call graph for this function:

Here is the caller graph for this function:

ssize_t response_callback ( void *  cls,
void *  buffer,
size_t  max,
bool *  more 
)

Definition at line 441 of file proxy.c.

References get_from_buffer(), PRINT_INFO, PRINT_VERBOSE, and PRINT_VERBOSE2.

Referenced by curl_header_cb().

Here is the call graph for this function:

Here is the caller graph for this function:

static void response_done_callback ( void *  cls,
struct SPDY_Response response,
struct SPDY_Request request,
enum SPDY_RESPONSE_RESULT  status,
bool  streamopened 
)
static

Definition at line 506 of file proxy.c.

References cleanup(), NULL, SPDY_destroy_request(), SPDY_destroy_response(), and SPDY_RESPONSE_RESULT_SUCCESS.

Referenced by curl_header_cb(), and run().

Here is the call graph for this function:

Here is the caller graph for this function:

static void session_closed_cb ( void *  cls,
struct SPDY_Session session,
int  by_client 
)
static

Definition at line 386 of file proxy.c.

References NULL, PRINT_VERBOSE2, and SPDY_get_cls_from_session().

Referenced by run().

Here is the call graph for this function:

Here is the caller graph for this function:

static int spdy_post_data_cb ( void *  cls,
struct SPDY_Request request,
const void *  buf,
size_t  size,
bool  more 
)
static

Definition at line 404 of file proxy.c.

References call_curl_run, PRINT_INFO, PRINT_INFO2, PRINT_VERBOSE, PRINT_VERBOSE2, SPDY_get_cls_from_request(), SPDY_YES, and store_in_buffer().

Referenced by run().

Here is the call graph for this function:

Here is the caller graph for this function:

static void standard_request_handler ( void *  cls,
struct SPDY_Request request,
uint8_t  priority,
const char *  method,
const char *  path,
const char *  version,
const char *  host,
const char *  scheme,
struct SPDY_NameValue headers,
bool  more 
)
static
static bool store_in_buffer ( const void *  src,
size_t  src_size,
void **  dst,
size_t *  dst_size 
)
static

Definition at line 312 of file proxy.c.

References NULL.

Referenced by curl_write_cb(), and spdy_post_data_cb().

Here is the caller graph for this function:

Variable Documentation

bool call_curl_run
static

Definition at line 154 of file proxy.c.

Referenced by run(), spdy_post_data_cb(), and standard_request_handler().

bool call_spdy_run
static

Definition at line 153 of file proxy.c.

Referenced by curl_header_cb(), curl_write_cb(), and run().

int debug_num_curls

Definition at line 156 of file proxy.c.

Referenced by cleanup(), run(), and standard_request_handler().

struct global_options glob_opt

Referenced by main(), run(), and standard_request_handler().

int loop = 1
static

Definition at line 145 of file proxy.c.

Referenced by catch_signal(), and run().

CURLM* multi_handle
static

Definition at line 147 of file proxy.c.

Referenced by cleanup(), run(), and standard_request_handler().

int still_running = 0
static

Definition at line 149 of file proxy.c.

Referenced by run(), and standard_request_handler().

regex_t uri_preg
static

Definition at line 151 of file proxy.c.

Referenced by run(), and standard_request_handler().