Commit 2da34f3d authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

http: helper for HTTP Basic authorization (refs #16166)

parent 364eefae
......@@ -32,6 +32,7 @@
#include <vlc_common.h>
#include <vlc_http.h>
#include <vlc_strings.h>
#include "message.h"
#include "h2frame.h"
......@@ -901,3 +902,41 @@ int vlc_http_msg_add_cookies(struct vlc_http_msg *m,
}
return val;
}
int vlc_http_msg_add_creds_basic(struct vlc_http_msg *m, bool proxy,
const char *username, const char *password)
{
char *str, *token;
int ret;
unsigned char c;
/* CTL characters and colons not permitted in username */
for (size_t len = 0; (c = username[len]) != '\0'; len++)
if (c < 32 || c == 127 || c == ':')
{
errno = EINVAL;
return -1;
}
/* CTL characters not permitted in password */
for (size_t len = 0; (c = password[len]) != '\0'; len++)
if (c < 32 || c == 127)
{
errno = EINVAL;
return -1;
}
ret = asprintf(&str, "%s:%s", username, password);
if (unlikely(ret < 0))
return -1;
token = vlc_b64_encode_binary((unsigned char *)str, ret);
free(str);
if (unlikely(token == NULL))
return -1;
ret = vlc_http_msg_add_header(m, proxy ? "Proxy-Authorization" :
"Authorization", "Basic %s", token);
free(token);
return ret;
}
......@@ -148,6 +148,23 @@ void vlc_http_msg_get_cookies(const struct vlc_http_msg *,
int vlc_http_msg_add_cookies(struct vlc_http_msg *,
struct vlc_http_cookie_jar_t *);
/**
* Adds Basic credentials.
*
* Formats a plain username and password pair using HTTP Basic (RFC7617)
* syntax.
*
* @param proxy true for proxy authentication,
* false for origin server authentication
* @param username null-terminated username
* @param password null-terminated password
* @return 0 on success, -1 on out-of-memory (ENOMEM) or if username or
* password are invalid (EINVAL).
*/
int vlc_http_msg_add_creds_basic(struct vlc_http_msg *, bool proxy,
const char *username, const char *password);
/**
* Looks up an header field.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment