src/detail/redirect.cpp
100.0% Lines (24/24)
100.0% List of functions (2/2)
100.0% Branches (19/19)
Functions (2)
| Line | Branch | TLA | Hits | Source Code |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Copyright (c) 2026 Mohammad Nejati | |||
| 3 | // | |||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||
| 6 | // | |||
| 7 | // Official repository: https://github.com/cppalliance/burl | |||
| 8 | // | |||
| 9 | ||||
| 10 | #include "redirect.hpp" | |||
| 11 | ||||
| 12 | #include <boost/http/field.hpp> | |||
| 13 | #include <boost/url/parse.hpp> | |||
| 14 | ||||
| 15 | namespace boost | |||
| 16 | { | |||
| 17 | namespace burl | |||
| 18 | { | |||
| 19 | namespace detail | |||
| 20 | { | |||
| 21 | ||||
| 22 | is_redirect_result | |||
| 23 | 49x | is_redirect( | ||
| 24 | http::status status, | |||
| 25 | const client::config& cfg) noexcept | |||
| 26 | { | |||
| 27 | // The specifications do not intend for 301 and 302 | |||
| 28 | // redirects to change the HTTP method, but most | |||
| 29 | // user agents do change the method in practice. | |||
| 30 |
5/5✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 26 times.
|
49x | switch(status) | |
| 31 | { | |||
| 32 | 4x | case http::status::moved_permanently: | ||
| 33 | 4x | return { true, !cfg.post301 }; | ||
| 34 | 12x | case http::status::found: | ||
| 35 | 12x | return { true, !cfg.post302 }; | ||
| 36 | 5x | case http::status::see_other: | ||
| 37 | 5x | return { true, !cfg.post303 }; | ||
| 38 | 2x | case http::status::temporary_redirect: | ||
| 39 | case http::status::permanent_redirect: | |||
| 40 | 2x | return { true, false }; | ||
| 41 | 26x | default: | ||
| 42 | 26x | return { false, false }; | ||
| 43 | } | |||
| 44 | } | |||
| 45 | ||||
| 46 | urls::url | |||
| 47 | 14x | resolve_location( | ||
| 48 | http::response_base const& response, | |||
| 49 | const urls::url_view& base) | |||
| 50 | { | |||
| 51 | 14x | auto it = response.find(http::field::location); | ||
| 52 |
2/2✓ Branch 2 taken 12 times.
✓ Branch 3 taken 2 times.
|
14x | if(it != response.end()) | |
| 53 | { | |||
| 54 |
1/1✓ Branch 3 taken 12 times.
|
12x | auto rs = urls::parse_uri_reference(it->value); | |
| 55 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 2 taken 1 time.
|
12x | if(rs.has_value()) | |
| 56 | { | |||
| 57 | 11x | urls::url url; | ||
| 58 |
2/2✓ Branch 2 taken 11 times.
✓ Branch 5 taken 11 times.
|
11x | urls::resolve(base, rs.value(), url); | |
| 59 | // RFC 9110, Section 10.2.2: a Location without a | |||
| 60 | // fragment inherits the fragment of the target URI. | |||
| 61 |
6/6✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 10 times.
|
11x | if(!url.has_fragment() && base.has_fragment()) | |
| 62 |
1/1✓ Branch 2 taken 1 time.
|
1x | url.set_encoded_fragment(base.encoded_fragment()); | |
| 63 | 11x | return url; | ||
| 64 | 11x | } | ||
| 65 | } | |||
| 66 | 3x | return {}; | ||
| 67 | } | |||
| 68 | ||||
| 69 | } // namespace detail | |||
| 70 | } // namespace burl | |||
| 71 | } // namespace boost | |||
| 72 |