comparison httpauth.c @ 6389:054de75e4a49 libavformat

Make parse_key_value from httpauth a common lavf internal function
author mstorsjo
date Thu, 19 Aug 2010 14:49:53 +0000
parents 4518f83661f4
children
comparison
equal deleted inserted replaced
6388:8ba781dd8b2b 6389:054de75e4a49
25 #include "internal.h" 25 #include "internal.h"
26 #include "libavutil/random_seed.h" 26 #include "libavutil/random_seed.h"
27 #include "libavutil/md5.h" 27 #include "libavutil/md5.h"
28 #include "avformat.h" 28 #include "avformat.h"
29 #include <ctype.h> 29 #include <ctype.h>
30
31 static void parse_key_value(const char *params,
32 void (*callback_get_buf)(HTTPAuthState *state,
33 const char *key, int key_len,
34 char **dest, int *dest_len), HTTPAuthState *state)
35 {
36 const char *ptr = params;
37
38 /* Parse key=value pairs. */
39 for (;;) {
40 const char *key;
41 char *dest = NULL, *dest_end;
42 int key_len, dest_len = 0;
43
44 /* Skip whitespace and potential commas. */
45 while (*ptr && (isspace(*ptr) || *ptr == ','))
46 ptr++;
47 if (!*ptr)
48 break;
49
50 key = ptr;
51
52 if (!(ptr = strchr(key, '=')))
53 break;
54 ptr++;
55 key_len = ptr - key;
56
57 callback_get_buf(state, key, key_len, &dest, &dest_len);
58 dest_end = dest + dest_len - 1;
59
60 if (*ptr == '\"') {
61 ptr++;
62 while (*ptr && *ptr != '\"') {
63 if (*ptr == '\\') {
64 if (!ptr[1])
65 break;
66 if (dest && dest < dest_end)
67 *dest++ = ptr[1];
68 ptr += 2;
69 } else {
70 if (dest && dest < dest_end)
71 *dest++ = *ptr;
72 ptr++;
73 }
74 }
75 if (*ptr == '\"')
76 ptr++;
77 } else {
78 for (; *ptr && !(isspace(*ptr) || *ptr == ','); ptr++)
79 if (dest && dest < dest_end)
80 *dest++ = *ptr;
81 }
82 if (dest)
83 *dest = 0;
84 }
85 }
86 30
87 static void handle_basic_params(HTTPAuthState *state, const char *key, 31 static void handle_basic_params(HTTPAuthState *state, const char *key,
88 int key_len, char **dest, int *dest_len) 32 int key_len, char **dest, int *dest_len)
89 { 33 {
90 if (!strncmp(key, "realm=", key_len)) { 34 if (!strncmp(key, "realm=", key_len)) {
147 const char *p; 91 const char *p;
148 if (av_stristart(value, "Basic ", &p) && 92 if (av_stristart(value, "Basic ", &p) &&
149 state->auth_type <= HTTP_AUTH_BASIC) { 93 state->auth_type <= HTTP_AUTH_BASIC) {
150 state->auth_type = HTTP_AUTH_BASIC; 94 state->auth_type = HTTP_AUTH_BASIC;
151 state->realm[0] = 0; 95 state->realm[0] = 0;
152 parse_key_value(p, handle_basic_params, state); 96 ff_parse_key_value(p, (ff_parse_key_val_cb) handle_basic_params,
97 state);
153 } else if (av_stristart(value, "Digest ", &p) && 98 } else if (av_stristart(value, "Digest ", &p) &&
154 state->auth_type <= HTTP_AUTH_DIGEST) { 99 state->auth_type <= HTTP_AUTH_DIGEST) {
155 state->auth_type = HTTP_AUTH_DIGEST; 100 state->auth_type = HTTP_AUTH_DIGEST;
156 memset(&state->digest_params, 0, sizeof(DigestParams)); 101 memset(&state->digest_params, 0, sizeof(DigestParams));
157 state->realm[0] = 0; 102 state->realm[0] = 0;
158 parse_key_value(p, handle_digest_params, state); 103 ff_parse_key_value(p, (ff_parse_key_val_cb) handle_digest_params,
104 state);
159 choose_qop(state->digest_params.qop, 105 choose_qop(state->digest_params.qop,
160 sizeof(state->digest_params.qop)); 106 sizeof(state->digest_params.qop));
161 } 107 }
162 } else if (!strcmp(key, "Authentication-Info")) { 108 } else if (!strcmp(key, "Authentication-Info")) {
163 parse_key_value(value, handle_digest_update, state); 109 ff_parse_key_value(value, (ff_parse_key_val_cb) handle_digest_update,
110 state);
164 } 111 }
165 } 112 }
166 113
167 114
168 static void update_md5_strings(struct AVMD5 *md5ctx, ...) 115 static void update_md5_strings(struct AVMD5 *md5ctx, ...)