~ubuntu-branches/ubuntu/trusty/serf/trusty-security

« back to all changes in this revision

Viewing changes to buckets/headers_buckets.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Samuelson
  • Date: 2011-06-03 03:18:07 UTC
  • mfrom: (1.2.4 upstream)
  • mto: (3.3.1 sid)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20110603031807-3vq82t5lzw692our
Tags: 0.7.2-1
* New upstream release.
  - patches/no-export-vars: delete, now upstream.
* New ABI:
  - patches/abi-0.x: New patch to change upstream SONAME.
  - Rename libserf-0-0 to libserf0.7.
  - Rename libserf-0-0-dev to libserf-dev while we're at it.
* Policy 3.9.1: one instance of s/Conflicts/Breaks/.
* "Upgrade" to source format 1.0.
* Add some Depends: ${misc:Depends}; thanks, Lintian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
} headers_context_t;
54
54
 
55
55
 
56
 
SERF_DECLARE(serf_bucket_t *) serf_bucket_headers_create(
 
56
serf_bucket_t *serf_bucket_headers_create(
57
57
    serf_bucket_alloc_t *allocator)
58
58
{
59
59
    headers_context_t *ctx;
65
65
    return serf_bucket_create(&serf_bucket_type_headers, allocator, ctx);
66
66
}
67
67
 
68
 
SERF_DECLARE(void) serf_bucket_headers_setx(
 
68
void serf_bucket_headers_setx(
69
69
    serf_bucket_t *bkt,
70
70
    const char *header, apr_size_t header_size, int header_copy,
71
71
    const char *value, apr_size_t value_size, int value_copy)
72
72
{
73
73
    headers_context_t *ctx = bkt->data;
74
 
    header_list_t *found = ctx->list;
 
74
    header_list_t *iter = ctx->list;
75
75
    header_list_t *hdr;
76
76
 
77
 
    /* Check to see if this header is already present. */
78
 
    while (found) {
79
 
      if (strncasecmp(found->header, header, header_size) == 0)
80
 
            break;
81
 
      found = found->next;
82
 
    }
83
 
 
84
 
    if (found) {
85
 
 
86
 
        /* The header is already present.  RFC 2616, section 4.2
87
 
           indicates that we should append the new value, separated by
88
 
           a comma.  Reasoning: for headers whose values are known to
89
 
           be comma-separated, that is clearly the correct behavior;
90
 
           for others, the correct behavior is undefined anyway. */
91
 
 
92
 
        /* The "+1" is for the comma; serf_bstrmemdup() will also add
93
 
           one slot for the terminating '\0'. */
94
 
        apr_size_t new_size = found->value_size + value_size + 1;
95
 
        char *new_val = serf_bucket_mem_alloc(bkt->allocator, new_size);
96
 
        memcpy(new_val, found->value, found->value_size);
97
 
        new_val[found->value_size] = ',';
98
 
        memcpy(new_val + found->value_size + 1, value, value_size);
99
 
        new_val[new_size] = '\0';
100
 
        found->value = new_val;
101
 
        found->value_size = new_size;
102
 
        found->alloc_flags |= ALLOC_VALUE;
103
 
        return;
104
 
    }
105
 
 
106
 
    /* Else the header is not already present.  Add it to the bucket. */
107
 
 
108
77
#if 0
109
78
    /* ### include this? */
110
79
    if (ctx->cur_read) {
117
86
    hdr->header_size = header_size;
118
87
    hdr->value_size = value_size;
119
88
    hdr->alloc_flags = 0;
 
89
    hdr->next = NULL;
120
90
 
121
91
    if (header_copy) {
122
92
        hdr->header = serf_bstrmemdup(bkt->allocator, header, header_size);
134
104
        hdr->value = value;
135
105
    }
136
106
 
137
 
    hdr->next = ctx->list;
138
 
    ctx->list = hdr;
 
107
    /* Add the new header at the end of the list. */
 
108
    while (iter && iter->next) {
 
109
        iter = iter->next;
 
110
    }
 
111
    if (iter)
 
112
        iter->next = hdr;
 
113
    else
 
114
        ctx->list = hdr;
139
115
}
140
116
 
141
 
SERF_DECLARE(void) serf_bucket_headers_set(
 
117
void serf_bucket_headers_set(
142
118
    serf_bucket_t *headers_bucket,
143
119
    const char *header,
144
120
    const char *value)
148
124
                             value, strlen(value), 1);
149
125
}
150
126
 
151
 
SERF_DECLARE(void) serf_bucket_headers_setc(
 
127
void serf_bucket_headers_setc(
152
128
    serf_bucket_t *headers_bucket,
153
129
    const char *header,
154
130
    const char *value)
158
134
                             value, strlen(value), 1);
159
135
}
160
136
 
161
 
SERF_DECLARE(void) serf_bucket_headers_setn(
 
137
void serf_bucket_headers_setn(
162
138
    serf_bucket_t *headers_bucket,
163
139
    const char *header,
164
140
    const char *value)
168
144
                             value, strlen(value), 0);
169
145
}
170
146
 
171
 
SERF_DECLARE(const char *) serf_bucket_headers_get(
 
147
const char *serf_bucket_headers_get(
172
148
    serf_bucket_t *headers_bucket,
173
149
    const char *header)
174
150
{
175
151
    headers_context_t *ctx = headers_bucket->data;
176
 
    header_list_t *scan = ctx->list;
177
 
 
178
 
    while (scan) {
179
 
        if (strcasecmp(scan->header, header) == 0)
180
 
            return scan->value;
181
 
        scan = scan->next;
 
152
    header_list_t *found = ctx->list;
 
153
    const char *val = NULL;
 
154
    int value_size = 0;
 
155
    int val_alloc = 0;
 
156
 
 
157
    while (found) {
 
158
        if (strcasecmp(found->header, header) == 0) {
 
159
            if (val) {
 
160
                /* The header is already present.  RFC 2616, section 4.2
 
161
                   indicates that we should append the new value, separated by
 
162
                   a comma.  Reasoning: for headers whose values are known to
 
163
                   be comma-separated, that is clearly the correct behavior;
 
164
                   for others, the correct behavior is undefined anyway. */
 
165
 
 
166
                /* The "+1" is for the comma; serf_bstrmemdup() will also add
 
167
                   one slot for the terminating '\0'. */
 
168
                apr_size_t new_size = found->value_size + value_size + 1;
 
169
                char *new_val = serf_bucket_mem_alloc(headers_bucket->allocator,
 
170
                                                      new_size);
 
171
                memcpy(new_val, val, value_size);
 
172
                new_val[value_size] = ',';
 
173
                memcpy(new_val + value_size + 1, found->value,
 
174
                       found->value_size);
 
175
                new_val[new_size] = '\0';
 
176
                /* Copy the new value over the already existing value. */
 
177
                if (val_alloc)
 
178
                    serf_bucket_mem_free(headers_bucket->allocator, (void*)val);
 
179
                val_alloc |= ALLOC_VALUE;
 
180
                val = new_val;
 
181
                value_size = new_size;
 
182
            }
 
183
            else {
 
184
                val = found->value;
 
185
                value_size = found->value_size;
 
186
            }
 
187
        }
 
188
        found = found->next;
182
189
    }
183
190
 
184
 
    return NULL;
 
191
    return val;
185
192
}
186
193
 
187
 
SERF_DECLARE(void) serf_bucket_headers_do(
 
194
void serf_bucket_headers_do(
188
195
    serf_bucket_t *headers_bucket,
189
196
    serf_bucket_headers_do_callback_fn_t func,
190
197
    void *baton)
410
417
    return APR_SUCCESS;
411
418
}
412
419
 
413
 
SERF_DECLARE_DATA const serf_bucket_type_t serf_bucket_type_headers = {
 
420
const serf_bucket_type_t serf_bucket_type_headers = {
414
421
    "HEADERS",
415
422
    serf_headers_read,
416
423
    serf_headers_readline,