Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add HTTP request connection with authorization #33

Merged
merged 12 commits into from
Oct 18, 2020
1 change: 1 addition & 0 deletions include/re_dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ struct dnsc_conf {

int dnsc_alloc(struct dnsc **dcpp, const struct dnsc_conf *conf,
const struct sa *srvv, uint32_t srvc);
int dnsc_conf_set(struct dnsc *dnsc, const struct dnsc_conf *conf);
int dnsc_srv_set(struct dnsc *dnsc, const struct sa *srvv, uint32_t srvc);
int dnsc_query(struct dns_query **qp, struct dnsc *dnsc, const char *name,
uint16_t type, uint16_t dnsclass,
Expand Down
49 changes: 44 additions & 5 deletions include/re_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ struct http_msg {
uint32_t clen; /**< Content length */
};

struct http_uri {
struct pl scheme;
struct pl host;
struct pl port;
struct pl path;
};

int http_uri_decode(struct http_uri *hu, const struct pl *uri);

typedef bool(http_hdr_h)(const struct http_hdr *hdr, void *arg);

int http_msg_decode(struct http_msg **msgp, struct mbuf *mb, bool req);
Expand Down Expand Up @@ -125,15 +134,24 @@ typedef void (http_conn_h)(struct tcp_conn *tc, struct tls_conn *sc,
void *arg);

int http_client_alloc(struct http_cli **clip, struct dnsc *dnsc);
int http_client_add_ca(struct http_cli *cli, const char *tls_ca);
int http_client_set_tls_hostname(struct http_cli *cli,
const struct pl *hostname);
int http_client_set_timeout(struct http_cli *cli, uint32_t ms);
int http_request(struct http_req **reqp, struct http_cli *cli, const char *met,
const char *uri, http_resp_h *resph, http_data_h *datah,
void *arg, const char *fmt, ...);
void http_req_set_conn_handler(struct http_req *req, http_conn_h *connh);
void http_client_set_laddr(struct http_cli *cli, struct sa *addr);
void http_client_set_laddr6(struct http_cli *cli, struct sa *addr);
void http_client_set_laddr(struct http_cli *cli, const struct sa *addr);
void http_client_set_laddr6(struct http_cli *cli, const struct sa *addr);

#ifdef USE_TLS
int http_client_add_ca(struct http_cli *cli, const char *tls_ca);
int http_client_add_capem(struct http_cli *cli, const char *capem);
int http_client_set_tls_hostname(struct http_cli *cli,
const struct pl *hostname);
int http_client_set_cert(struct http_cli *cli, const char *path);
int http_client_set_certpem(struct http_cli *cli, const char *pem);
int http_client_set_key(struct http_cli *cli, const char *path);
int http_client_set_keypem(struct http_cli *cli, const char *pem);
#endif

/* Server */
struct http_sock;
Expand Down Expand Up @@ -173,3 +191,24 @@ bool http_auth_check(const struct pl *hval, const struct pl *method,
bool http_auth_check_request(const struct http_msg *msg,
struct http_auth *auth,
http_auth_h *authh, void *arg);

/* http_reqconn - HTTP request connection */
struct http_reqconn;
int http_reqconn_alloc(struct http_reqconn **pconn,
struct http_cli *client,
http_resp_h *resph, http_data_h *datah, void* arg);
int http_reqconn_set_auth(struct http_reqconn *conn, const struct pl *user,
const struct pl *pass);
int http_reqconn_set_bearer(struct http_reqconn *conn,
const struct pl *bearer);
int http_reqconn_set_method(struct http_reqconn *conn, const struct pl *met);
int http_reqconn_set_body(struct http_reqconn *conn, const struct pl *body);
int http_reqconn_set_ctype(struct http_reqconn *conn, const struct pl *ctype);
int http_reqconn_add_header(struct http_reqconn *conn,
const struct pl *header);
int http_reqconn_clr_header(struct http_reqconn *conn);
int http_reqconn_send(struct http_reqconn *conn, const struct pl *uri);
#ifdef USE_TLS
int http_reqconn_set_tls_hostname(struct http_reqconn *conn,
const struct pl *hostname);
#endif
23 changes: 23 additions & 0 deletions include/re_httpauth.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ struct httpauth_digest_resp {
struct pl nc;
struct pl cnonce;
struct pl qop;

struct mbuf *mb;
};


/** HTTP Basic */
struct httpauth_basic {
struct mbuf *mb;
struct pl realm;
struct pl auth;
};


Expand All @@ -38,3 +48,16 @@ int httpauth_digest_response_decode(struct httpauth_digest_resp *resp,
const struct pl *hval);
int httpauth_digest_response_auth(const struct httpauth_digest_resp *resp,
const struct pl *method, const uint8_t *ha1);
int httpauth_digest_make_response(struct httpauth_digest_resp **resp,
const struct httpauth_digest_chall *chall,
const char *path, const char *method, const char *user,
const char *pwd, const char *body);
int httpauth_digest_response_encode(const struct httpauth_digest_resp *resp,
struct mbuf *mb);

struct httpauth_basic *httpauth_basic_alloc(void);
int httpauth_basic_decode(struct httpauth_basic *basic,
const struct pl *hval);
int httpauth_basic_make_response(struct httpauth_basic *basic,
const char *user, const char *pwd);
int httpauth_basic_encode(const struct httpauth_basic *basic, struct mbuf *mb);
2 changes: 1 addition & 1 deletion include/re_tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum tls_keytype {
int tls_alloc(struct tls **tlsp, enum tls_method method, const char *keyfile,
const char *pwd);
int tls_add_ca(struct tls *tls, const char *cafile);
int tls_add_capem(struct tls *tls, const char *capem);
int tls_set_selfsigned(struct tls *tls, const char *cn);
int tls_set_selfsigned_rsa(struct tls *tls, const char *cn, size_t bits);
int tls_set_certificate_pem(struct tls *tls, const char *cert, size_t len_cert,
Expand All @@ -51,7 +52,6 @@ int tls_peer_fingerprint(const struct tls_conn *tc, enum tls_fingerprint type,
int tls_peer_common_name(const struct tls_conn *tc, char *cn, size_t size);
int tls_peer_set_verify_host(struct tls_conn *tc, const char *hostname);
int tls_set_verify_purpose(struct tls *tls, const char *purpose);
int tls_set_hostname(char *tls_hostname, const struct pl *hostname);
int tls_peer_verify(const struct tls_conn *tc);
int tls_srtp_keyinfo(const struct tls_conn *tc, enum srtp_suite *suite,
uint8_t *cli_key, size_t cli_key_size,
Expand Down
27 changes: 27 additions & 0 deletions src/dns/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,33 @@ int dnsc_alloc(struct dnsc **dcpp, const struct dnsc_conf *conf,
}


int dnsc_conf_set(struct dnsc *dnsc, const struct dnsc_conf *conf)
{
int err;
if (!dnsc)
return EINVAL;

if (conf)
dnsc->conf = *conf;
else
dnsc->conf = default_conf;


dnsc->ht_query = mem_deref(dnsc->ht_query);
dnsc->ht_tcpconn = mem_deref(dnsc->ht_tcpconn);

err = hash_alloc(&dnsc->ht_query, dnsc->conf.query_hash_size);
if (err)
return err;

err = hash_alloc(&dnsc->ht_tcpconn, dnsc->conf.tcp_hash_size);
if (err)
return err;

return err;
}


/**
* Set the DNS Servers on a DNS Client
*
Expand Down
Loading