1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::{Encoding, Expectation, MessageHeadersType};
use glib::translate::*;

glib::wrapper! {
    /// The HTTP message headers associated with a request or response.
    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct MessageHeaders(Shared<ffi::SoupMessageHeaders>);

    match fn {
        ref => |ptr| ffi::soup_message_headers_ref(ptr),
        unref => |ptr| ffi::soup_message_headers_unref(ptr),
        type_ => || ffi::soup_message_headers_get_type(),
    }
}

impl MessageHeaders {
    /// Creates a #SoupMessageHeaders.
    ///
    /// ([`Message`][crate::Message] does this automatically for its own headers. You would only
    /// need to use this method if you are manually parsing or generating message
    /// headers.)
    /// ## `type_`
    /// the type of headers
    ///
    /// # Returns
    ///
    /// a new #SoupMessageHeaders
    #[doc(alias = "soup_message_headers_new")]
    pub fn new(type_: MessageHeadersType) -> MessageHeaders {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::soup_message_headers_new(type_.into_glib())) }
    }

    /// Appends a new header with name @name and value @value to @self.
    ///
    /// (If there is an existing header with name @name, then this creates a second
    /// one, which is only allowed for list-valued headers; see also
    /// [`replace()`][Self::replace()].)
    ///
    /// The caller is expected to make sure that @name and @value are
    /// syntactically correct.
    /// ## `name`
    /// the header name to add
    /// ## `value`
    /// the new value of @name
    #[doc(alias = "soup_message_headers_append")]
    pub fn append(&self, name: &str, value: &str) {
        unsafe {
            ffi::soup_message_headers_append(
                self.to_glib_none().0,
                name.to_glib_none().0,
                value.to_glib_none().0,
            );
        }
    }

    /// Removes all the headers listed in the Connection header.
    #[doc(alias = "soup_message_headers_clean_connection_headers")]
    pub fn clean_connection_headers(&self) {
        unsafe {
            ffi::soup_message_headers_clean_connection_headers(self.to_glib_none().0);
        }
    }

    /// Clears @self.
    #[doc(alias = "soup_message_headers_clear")]
    pub fn clear(&self) {
        unsafe {
            ffi::soup_message_headers_clear(self.to_glib_none().0);
        }
    }

    /// Calls @func once for each header value in @self.
    ///
    /// Beware that unlike [`list()`][Self::list()], this processes the
    /// headers in exactly the way they were added, rather than
    /// concatenating multiple same-named headers into a single value.
    /// (This is intentional; it ensures that if you call
    /// [`append()`][Self::append()] multiple times with the same name,
    /// then the I/O code will output multiple copies of the header when
    /// sending the message to the remote implementation, which may be
    /// required for interoperability in some cases.)
    ///
    /// You may not modify the headers from @func.
    /// ## `func`
    /// callback function to run for each header
    #[doc(alias = "soup_message_headers_foreach")]
    pub fn foreach<P: FnMut(&str, &str)>(&self, func: P) {
        let func_data: P = func;
        unsafe extern "C" fn func_func<P: FnMut(&str, &str)>(
            name: *const libc::c_char,
            value: *const libc::c_char,
            user_data: glib::ffi::gpointer,
        ) {
            let name: Borrowed<glib::GString> = from_glib_borrow(name);
            let value: Borrowed<glib::GString> = from_glib_borrow(value);
            let callback = user_data as *mut P;
            (*callback)(name.as_str(), value.as_str())
        }
        let func = Some(func_func::<P> as _);
        let super_callback0: &P = &func_data;
        unsafe {
            ffi::soup_message_headers_foreach(
                self.to_glib_none().0,
                func,
                super_callback0 as *const _ as *mut _,
            );
        }
    }

    //#[doc(alias = "soup_message_headers_free_ranges")]
    //pub fn free_ranges(&self, ranges: /*Ignored*/&mut Range) {
    //    unsafe { TODO: call ffi:soup_message_headers_free_ranges() }
    //}

    /// Gets the message body length that @self declare.
    ///
    /// This will only be non-0 if [`encoding()`][Self::encoding()] returns
    /// [`Encoding::ContentLength`][crate::Encoding::ContentLength].
    ///
    /// # Returns
    ///
    /// the message body length declared by @self.
    #[doc(alias = "soup_message_headers_get_content_length")]
    #[doc(alias = "get_content_length")]
    pub fn content_length(&self) -> i64 {
        unsafe { ffi::soup_message_headers_get_content_length(self.to_glib_none().0) }
    }

    /// Parses @self's Content-Range header and returns it in @start,
    /// @end, and @total_length. If the total length field in the header
    /// was specified as "*", then @total_length will be set to -1.
    ///
    /// # Returns
    ///
    /// [`true`] if @self contained a "Content-Range" header
    ///   containing a byte range which could be parsed, [`false`] otherwise.
    ///
    /// ## `start`
    /// return value for the start of the range
    ///
    /// ## `end`
    /// return value for the end of the range
    ///
    /// ## `total_length`
    /// return value for the total length of the
    ///   resource, or [`None`] if you don't care.
    #[doc(alias = "soup_message_headers_get_content_range")]
    #[doc(alias = "get_content_range")]
    pub fn content_range(&self) -> Option<(i64, i64, i64)> {
        unsafe {
            let mut start = std::mem::MaybeUninit::uninit();
            let mut end = std::mem::MaybeUninit::uninit();
            let mut total_length = std::mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::soup_message_headers_get_content_range(
                self.to_glib_none().0,
                start.as_mut_ptr(),
                end.as_mut_ptr(),
                total_length.as_mut_ptr(),
            ));
            if ret {
                Some((
                    start.assume_init(),
                    end.assume_init(),
                    total_length.assume_init(),
                ))
            } else {
                None
            }
        }
    }

    /// Gets the message body encoding that @self declare.
    ///
    /// This may not always correspond to the encoding used on the wire; eg, a HEAD
    /// response may declare a Content-Length or Transfer-Encoding, but it will never
    /// actually include a body.
    ///
    /// # Returns
    ///
    /// the encoding declared by @self.
    #[doc(alias = "soup_message_headers_get_encoding")]
    #[doc(alias = "get_encoding")]
    pub fn encoding(&self) -> Encoding {
        unsafe {
            from_glib(ffi::soup_message_headers_get_encoding(
                self.to_glib_none().0,
            ))
        }
    }

    /// Gets the expectations declared by @self's "Expect" header.
    ///
    /// Currently this will either be [`Expectation::CONTINUE`][crate::Expectation::CONTINUE] or
    /// [`Expectation::UNRECOGNIZED`][crate::Expectation::UNRECOGNIZED].
    ///
    /// # Returns
    ///
    /// the contents of @self's "Expect" header
    #[doc(alias = "soup_message_headers_get_expectations")]
    #[doc(alias = "get_expectations")]
    pub fn expectations(&self) -> Expectation {
        unsafe {
            from_glib(ffi::soup_message_headers_get_expectations(
                self.to_glib_none().0,
            ))
        }
    }

    /// Gets the type of headers.
    ///
    /// # Returns
    ///
    /// the header's type.
    #[doc(alias = "soup_message_headers_get_headers_type")]
    #[doc(alias = "get_headers_type")]
    pub fn headers_type(&self) -> MessageHeadersType {
        unsafe {
            from_glib(ffi::soup_message_headers_get_headers_type(
                self.to_glib_none().0,
            ))
        }
    }

    /// Gets the value of header @name in @self.
    ///
    /// Use this for headers whose values are comma-delimited lists, and which are
    /// therefore allowed to appear multiple times in the headers. For
    /// non-list-valued headers, use [`one()`][Self::one()].
    ///
    /// If @name appears multiple times in @self,
    /// [`list()`][Self::list()] will concatenate all of the values
    /// together, separated by commas. This is sometimes awkward to parse
    /// (eg, WWW-Authenticate, Set-Cookie), but you have to be able to deal
    /// with it anyway, because the HTTP spec explicitly states that this
    /// transformation is allowed, and so an upstream proxy could do the
    /// same thing.
    /// ## `name`
    /// header name
    ///
    /// # Returns
    ///
    /// the header's value or [`None`] if not found.
    #[doc(alias = "soup_message_headers_get_list")]
    #[doc(alias = "get_list")]
    pub fn list(&self, name: &str) -> Option<glib::GString> {
        unsafe {
            from_glib_none(ffi::soup_message_headers_get_list(
                self.to_glib_none().0,
                name.to_glib_none().0,
            ))
        }
    }

    /// Gets the value of header @name in @self.
    ///
    /// Use this for headers whose values are *not* comma-delimited lists, and which
    /// therefore can only appear at most once in the headers. For list-valued
    /// headers, use [`list()`][Self::list()].
    ///
    /// If @self does erroneously contain multiple copies of the header, it
    /// is not defined which one will be returned. (Ideally, it will return
    /// whichever one makes libsoup most compatible with other HTTP
    /// implementations.)
    /// ## `name`
    /// header name
    ///
    /// # Returns
    ///
    /// the header's value or [`None`] if not found.
    #[doc(alias = "soup_message_headers_get_one")]
    #[doc(alias = "get_one")]
    pub fn one(&self, name: &str) -> Option<glib::GString> {
        unsafe {
            from_glib_none(ffi::soup_message_headers_get_one(
                self.to_glib_none().0,
                name.to_glib_none().0,
            ))
        }
    }

    //#[doc(alias = "soup_message_headers_get_ranges")]
    //#[doc(alias = "get_ranges")]
    //pub fn ranges(&self, total_length: i64, ranges: /*Ignored*/Vec<Range>) -> Option<i32> {
    //    unsafe { TODO: call ffi:soup_message_headers_get_ranges() }
    //}

    /// Checks whether the list-valued header @name is present in @self,
    /// and contains a case-insensitive match for @token.
    ///
    /// (If @name is present in @self, then this is equivalent to calling
    /// [`header_contains()`][Self::header_contains()] on its value.)
    /// ## `name`
    /// header name
    /// ## `token`
    /// token to look for
    ///
    /// # Returns
    ///
    /// [`true`] if the header is present and contains @token,
    ///   [`false`] otherwise.
    #[doc(alias = "soup_message_headers_header_contains")]
    pub fn header_contains(&self, name: &str, token: &str) -> bool {
        unsafe {
            from_glib(ffi::soup_message_headers_header_contains(
                self.to_glib_none().0,
                name.to_glib_none().0,
                token.to_glib_none().0,
            ))
        }
    }

    /// Checks whether the header @name is present in @self and is
    /// (case-insensitively) equal to @value.
    /// ## `name`
    /// header name
    /// ## `value`
    /// expected value
    ///
    /// # Returns
    ///
    /// [`true`] if the header is present and its value is
    ///   @value, [`false`] otherwise.
    #[doc(alias = "soup_message_headers_header_equals")]
    pub fn header_equals(&self, name: &str, value: &str) -> bool {
        unsafe {
            from_glib(ffi::soup_message_headers_header_equals(
                self.to_glib_none().0,
                name.to_glib_none().0,
                value.to_glib_none().0,
            ))
        }
    }

    /// Removes @name from @self.
    ///
    /// If there are multiple values for @name, they are all removed.
    /// ## `name`
    /// the header name to remove
    #[doc(alias = "soup_message_headers_remove")]
    pub fn remove(&self, name: &str) {
        unsafe {
            ffi::soup_message_headers_remove(self.to_glib_none().0, name.to_glib_none().0);
        }
    }

    /// Replaces the value of the header @name in @self with @value.
    ///
    /// See also [`append()`][Self::append()].
    ///
    /// The caller is expected to make sure that @name and @value are
    /// syntactically correct.
    /// ## `name`
    /// the header name to replace
    /// ## `value`
    /// the new value of @name
    #[doc(alias = "soup_message_headers_replace")]
    pub fn replace(&self, name: &str, value: &str) {
        unsafe {
            ffi::soup_message_headers_replace(
                self.to_glib_none().0,
                name.to_glib_none().0,
                value.to_glib_none().0,
            );
        }
    }

    /// Sets the message body length that @self will declare, and sets
    /// @self's encoding to [`Encoding::ContentLength`][crate::Encoding::ContentLength].
    ///
    /// You do not normally need to call this; if @self is set to use
    /// Content-Length encoding, libsoup will automatically set its
    /// Content-Length header for you immediately before sending the
    /// headers. One situation in which this method is useful is when
    /// generating the response to a HEAD request; Calling
    /// [`set_content_length()`][Self::set_content_length()] allows you to put the
    /// correct content length into the response without needing to waste
    /// memory by filling in a response body which won't actually be sent.
    /// ## `content_length`
    /// the message body length
    #[doc(alias = "soup_message_headers_set_content_length")]
    pub fn set_content_length(&self, content_length: i64) {
        unsafe {
            ffi::soup_message_headers_set_content_length(self.to_glib_none().0, content_length);
        }
    }

    /// Sets @self's Content-Range header according to the given values.
    ///
    /// (Note that @total_length is the total length of the entire resource
    /// that this is a range of, not simply @end - @start + 1.)
    ///
    /// [`Server`][crate::Server] has built-in handling for range requests, and you do
    /// not normally need to call this function youself. See
    /// `MessageHeaders::get_ranges()` for more details.
    /// ## `start`
    /// the start of the range
    /// ## `end`
    /// the end of the range
    /// ## `total_length`
    /// the total length of the resource, or -1 if unknown
    #[doc(alias = "soup_message_headers_set_content_range")]
    pub fn set_content_range(&self, start: i64, end: i64, total_length: i64) {
        unsafe {
            ffi::soup_message_headers_set_content_range(
                self.to_glib_none().0,
                start,
                end,
                total_length,
            );
        }
    }

    /// Sets the message body encoding that @self will declare.
    ///
    /// In particular, you should use this if you are going to send a request or
    /// response in chunked encoding.
    /// ## `encoding`
    /// a #SoupEncoding
    #[doc(alias = "soup_message_headers_set_encoding")]
    pub fn set_encoding(&self, encoding: Encoding) {
        unsafe {
            ffi::soup_message_headers_set_encoding(self.to_glib_none().0, encoding.into_glib());
        }
    }

    /// Sets @self's "Expect" header according to @expectations.
    ///
    /// Currently [`Expectation::CONTINUE`][crate::Expectation::CONTINUE] is the only known expectation
    /// value. You should set this value on a request if you are sending a
    /// large message body (eg, via POST or PUT), and want to give the
    /// server a chance to reject the request after seeing just the headers
    /// (eg, because it will require authentication before allowing you to
    /// post, or because you're POSTing to a URL that doesn't exist). This
    /// saves you from having to transmit the large request body when the
    /// server is just going to ignore it anyway.
    /// ## `expectations`
    /// the expectations to set
    #[doc(alias = "soup_message_headers_set_expectations")]
    pub fn set_expectations(&self, expectations: Expectation) {
        unsafe {
            ffi::soup_message_headers_set_expectations(
                self.to_glib_none().0,
                expectations.into_glib(),
            );
        }
    }

    /// Sets @self's Range header to request the indicated range.
    ///
    /// @start and @end are interpreted as in a `Range`.
    ///
    /// If you need to request multiple ranges, use
    /// `MessageHeaders::set_ranges()`.
    /// ## `start`
    /// the start of the range to request
    /// ## `end`
    /// the end of the range to request
    #[doc(alias = "soup_message_headers_set_range")]
    pub fn set_range(&self, start: i64, end: i64) {
        unsafe {
            ffi::soup_message_headers_set_range(self.to_glib_none().0, start, end);
        }
    }

    //#[doc(alias = "soup_message_headers_set_ranges")]
    //pub fn set_ranges(&self, ranges: /*Ignored*/&mut Range, length: i32) {
    //    unsafe { TODO: call ffi:soup_message_headers_set_ranges() }
    //}
}