Skip to content

Commit

Permalink
lib-http: client: Cleaned up headers and added some more documentatio…
Browse files Browse the repository at this point in the history
…n there.
  • Loading branch information
stephanbosch authored and sirainen committed Nov 9, 2016
1 parent 0c43fc2 commit 390a66a
Show file tree
Hide file tree
Showing 2 changed files with 267 additions and 131 deletions.
234 changes: 136 additions & 98 deletions src/lib-http/http-client-private.h
Expand Up @@ -6,6 +6,10 @@
#include "http-url.h"
#include "http-client.h"

/*
* Defaults
*/

#define HTTP_DEFAULT_PORT 80
#define HTTPS_DEFAULT_PORT 443

Expand All @@ -15,6 +19,10 @@
#define HTTP_CLIENT_DEFAULT_BACKOFF_TIME_MSECS (100)
#define HTTP_CLIENT_DEFAULT_BACKOFF_MAX_TIME_MSECS (1000*60)

/*
* Types
*/

enum http_response_payload_type;

struct http_client_host;
Expand Down Expand Up @@ -55,6 +63,10 @@ struct http_client_peer_addr {
} a;
};

/*
* Objects
*/

struct http_client_request {
pool_t pool;
unsigned int refcount;
Expand Down Expand Up @@ -278,7 +290,78 @@ struct http_client {
unsigned int requests_count;
};

int http_client_init_ssl_ctx(struct http_client *client, const char **error_r);
/*
* Peer address
*/

static inline bool
http_client_peer_addr_is_https(const struct http_client_peer_addr *addr)
{
switch (addr->type) {
case HTTP_CLIENT_PEER_ADDR_HTTPS:
case HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL:
return TRUE;
default:
break;
}
return FALSE;
}

static inline const char *
http_client_peer_addr_get_https_name(const struct http_client_peer_addr *addr)
{
switch (addr->type) {
case HTTP_CLIENT_PEER_ADDR_HTTPS:
case HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL:
return addr->a.tcp.https_name;
default:
break;
}
return NULL;
}

static inline const char *
http_client_peer_addr2str(const struct http_client_peer_addr *addr)
{
switch (addr->type) {
case HTTP_CLIENT_PEER_ADDR_HTTP:
case HTTP_CLIENT_PEER_ADDR_HTTPS:
case HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL:
case HTTP_CLIENT_PEER_ADDR_RAW:
if (addr->a.tcp.ip.family == AF_INET6) {
return t_strdup_printf("[%s]:%u",
net_ip2addr(&addr->a.tcp.ip), addr->a.tcp.port);
}
return t_strdup_printf("%s:%u",
net_ip2addr(&addr->a.tcp.ip), addr->a.tcp.port);
case HTTP_CLIENT_PEER_ADDR_UNIX:
return t_strdup_printf("unix:%s", addr->a.un.path);
default:
break;
}
i_unreached();
return "";
}

/*
* Request
*/

static inline const char *
http_client_request_label(struct http_client_request *req)
{
if (req->label == NULL) {
return t_strdup_printf("[Req%u: %s %s%s]", req->id,
req->method, http_url_create(&req->origin_url), req->target);
}
return req->label;
}

static inline bool
http_client_request_to_proxy(const struct http_client_request *req)
{
return (req->host_url != &req->origin_url);
}

void http_client_request_ref(struct http_client_request *req);
/* Returns FALSE if unrefing destroyed the request entirely */
Expand Down Expand Up @@ -310,6 +393,19 @@ void http_client_request_redirect(struct http_client_request *req,
unsigned int status, const char *location);
void http_client_request_finish(struct http_client_request *req);

/*
* Connection
*/

static inline const char *
http_client_connection_label(struct http_client_connection *conn)
{
return t_strdup_printf("%s%s [%d]",
http_client_peer_addr2str(&conn->peer->addr),
(conn->peer->addr.type == HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL ?
" (tunnel)" : ""), conn->id);
}

struct connection_list *http_client_connection_list_init(void);

struct http_client_connection *
Expand Down Expand Up @@ -339,6 +435,19 @@ void http_client_connection_switch_ioloop(struct http_client_connection *conn);
void http_client_connection_start_tunnel(struct http_client_connection **_conn,
struct http_client_tunnel *tunnel);

/*
* Peer
*/

static inline const char *
http_client_peer_label(struct http_client_peer *peer)
{
if (peer->addr.type == HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL) {
return t_strconcat
(http_client_peer_addr2str(&peer->addr), " (tunnel)", NULL);
}
return http_client_peer_addr2str(&peer->addr);
}

unsigned int http_client_peer_addr_hash
(const struct http_client_peer_addr *peer) ATTR_PURE;
Expand Down Expand Up @@ -376,6 +485,10 @@ unsigned int
http_client_peer_pending_connections(struct http_client_peer *peer);
void http_client_peer_switch_ioloop(struct http_client_peer *peer);

/*
* Queue
*/

struct http_client_queue *
http_client_queue_create(struct http_client_host *host,
const struct http_client_peer_addr *addr);
Expand Down Expand Up @@ -403,103 +516,9 @@ void http_client_queue_peer_disconnected(struct http_client_queue *queue,
struct http_client_peer *peer);
void http_client_queue_switch_ioloop(struct http_client_queue *queue);

struct http_client_host *
http_client_host_get(struct http_client *client,
const struct http_url *host_url);
void http_client_host_free(struct http_client_host **_host);
void http_client_host_submit_request(struct http_client_host *host,
struct http_client_request *req);
void http_client_host_switch_ioloop(struct http_client_host *host);

void http_client_delay_request_error(struct http_client *client,
struct http_client_request *req);
void http_client_remove_request_error(struct http_client *client,
struct http_client_request *req);


static inline bool
http_client_peer_addr_is_https(const struct http_client_peer_addr *addr)
{
switch (addr->type) {
case HTTP_CLIENT_PEER_ADDR_HTTPS:
case HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL:
return TRUE;
default:
break;
}
return FALSE;
}

static inline const char *
http_client_peer_addr_get_https_name(const struct http_client_peer_addr *addr)
{
switch (addr->type) {
case HTTP_CLIENT_PEER_ADDR_HTTPS:
case HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL:
return addr->a.tcp.https_name;
default:
break;
}
return NULL;
}

static inline const char *
http_client_peer_addr2str(const struct http_client_peer_addr *addr)
{
switch (addr->type) {
case HTTP_CLIENT_PEER_ADDR_HTTP:
case HTTP_CLIENT_PEER_ADDR_HTTPS:
case HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL:
case HTTP_CLIENT_PEER_ADDR_RAW:
if (addr->a.tcp.ip.family == AF_INET6) {
return t_strdup_printf("[%s]:%u",
net_ip2addr(&addr->a.tcp.ip), addr->a.tcp.port);
}
return t_strdup_printf("%s:%u",
net_ip2addr(&addr->a.tcp.ip), addr->a.tcp.port);
case HTTP_CLIENT_PEER_ADDR_UNIX:
return t_strdup_printf("unix:%s", addr->a.un.path);
default:
break;
}
i_unreached();
return "";
}

static inline const char *
http_client_request_label(struct http_client_request *req)
{
if (req->label == NULL) {
return t_strdup_printf("[Req%u: %s %s%s]", req->id,
req->method, http_url_create(&req->origin_url), req->target);
}
return req->label;
}

static inline bool
http_client_request_to_proxy(const struct http_client_request *req)
{
return (req->host_url != &req->origin_url);
}

static inline const char *
http_client_connection_label(struct http_client_connection *conn)
{
return t_strdup_printf("%s%s [%d]",
http_client_peer_addr2str(&conn->peer->addr),
(conn->peer->addr.type == HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL ?
" (tunnel)" : ""), conn->id);
}

static inline const char *
http_client_peer_label(struct http_client_peer *peer)
{
if (peer->addr.type == HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL) {
return t_strconcat
(http_client_peer_addr2str(&peer->addr), " (tunnel)", NULL);
}
return http_client_peer_addr2str(&peer->addr);
}
/*
* Host
*/

static inline unsigned int
http_client_host_get_ip_idx(struct http_client_host *host,
Expand All @@ -514,5 +533,24 @@ http_client_host_get_ip_idx(struct http_client_host *host,
i_unreached();
}

struct http_client_host *
http_client_host_get(struct http_client *client,
const struct http_url *host_url);
void http_client_host_free(struct http_client_host **_host);
void http_client_host_submit_request(struct http_client_host *host,
struct http_client_request *req);
void http_client_host_switch_ioloop(struct http_client_host *host);

/*
* Client
*/

int http_client_init_ssl_ctx(struct http_client *client,
const char **error_r);

void http_client_delay_request_error(struct http_client *client,
struct http_client_request *req);
void http_client_remove_request_error(struct http_client *client,
struct http_client_request *req);

#endif

0 comments on commit 390a66a

Please sign in to comment.