annotate httpauth.c @ 5879:61062082488b libavformat

Split out http authentication handling into a separate file This prepares for adding support for more authentication methods
author mstorsjo
date Wed, 24 Mar 2010 22:32:05 +0000
parents
children a1a309c4a751
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5879
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
1 /*
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
2 * HTTP authentication
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
3 * Copyright (c) 2010 Martin Storsjo
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
4 *
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
5 * This file is part of FFmpeg.
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
6 *
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
11 *
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
15 * Lesser General Public License for more details.
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
16 *
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
20 */
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
21
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
22 #include "httpauth.h"
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
23 #include "libavutil/base64.h"
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
24 #include "libavutil/avstring.h"
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
25 #include "avformat.h"
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
26 #include <ctype.h>
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
27
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
28 static void parse_key_value(const char *params,
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
29 void (*callback_get_buf)(HTTPAuthState *state,
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
30 const char *key, int key_len,
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
31 char **dest, int *dest_len), HTTPAuthState *state)
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
32 {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
33 const char *ptr = params;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
34
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
35 /* Parse key=value pairs. */
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
36 for (;;) {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
37 const char *key;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
38 char *dest = NULL, *dest_end;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
39 int key_len, dest_len = 0;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
40
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
41 /* Skip whitespace and potential commas. */
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
42 while (*ptr && (isspace(*ptr) || *ptr == ','))
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
43 ptr++;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
44 if (!*ptr)
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
45 break;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
46
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
47 key = ptr;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
48
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
49 if (!(ptr = strchr(key, '=')))
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
50 break;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
51 ptr++;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
52 key_len = ptr - key;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
53
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
54 callback_get_buf(state, key, key_len, &dest, &dest_len);
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
55 dest_end = dest + dest_len - 1;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
56
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
57 if (*ptr == '\"') {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
58 ptr++;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
59 while (*ptr && *ptr != '\"') {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
60 if (*ptr == '\\') {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
61 if (!ptr[1])
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
62 break;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
63 if (dest && dest < dest_end)
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
64 *dest++ = ptr[1];
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
65 ptr += 2;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
66 } else {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
67 if (dest && dest < dest_end)
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
68 *dest++ = *ptr;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
69 ptr++;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
70 }
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
71 }
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
72 if (*ptr == '\"')
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
73 ptr++;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
74 } else {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
75 for (; *ptr && !(isspace(*ptr) || *ptr == ','); ptr++)
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
76 if (dest && dest < dest_end)
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
77 *dest++ = *ptr;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
78 }
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
79 if (dest)
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
80 *dest = 0;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
81 }
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
82 }
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
83
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
84 static void handle_basic_params(HTTPAuthState *state, const char *key,
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
85 int key_len, char **dest, int *dest_len)
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
86 {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
87 if (!strncmp(key, "realm=", key_len)) {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
88 *dest = state->realm;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
89 *dest_len = sizeof(state->realm);
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
90 }
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
91 }
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
92
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
93 void ff_http_auth_handle_header(HTTPAuthState *state, const char *key,
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
94 const char *value)
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
95 {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
96 if (!state)
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
97 return;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
98
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
99 if (!strcmp(key, "WWW-Authenticate")) {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
100 const char *p;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
101 if (av_stristart(value, "Basic ", &p) &&
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
102 state->auth_type <= HTTP_AUTH_BASIC) {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
103 state->auth_type = HTTP_AUTH_BASIC;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
104 state->realm[0] = 0;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
105 parse_key_value(p, handle_basic_params, state);
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
106 }
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
107 }
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
108 }
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
109
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
110 char *ff_http_auth_create_response(HTTPAuthState *state, const char *auth,
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
111 const char *path, const char *method)
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
112 {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
113 char *authstr = NULL;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
114
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
115 if (!auth || !strchr(auth, ':'))
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
116 return NULL;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
117
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
118 if (state->auth_type == HTTP_AUTH_BASIC) {
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
119 int auth_b64_len = (strlen(auth) + 2) / 3 * 4 + 1;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
120 int len = auth_b64_len + 30;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
121 char *ptr;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
122 authstr = av_malloc(len);
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
123 if (!authstr)
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
124 return NULL;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
125 snprintf(authstr, len, "Authorization: Basic ");
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
126 ptr = authstr + strlen(authstr);
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
127 av_base64_encode(ptr, auth_b64_len, auth, strlen(auth));
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
128 av_strlcat(ptr, "\r\n", len);
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
129 }
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
130 return authstr;
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
131 }
61062082488b Split out http authentication handling into a separate file
mstorsjo
parents:
diff changeset
132