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
|
//! ref: composer/src/Composer/Util/Http/Response.php
use crate::json::json_file::JsonFile;
use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::preg::Preg;
use shirabe_php_shim::{LogicException, PhpMixed, preg_quote};
#[derive(Debug)]
pub struct Response {
request: IndexMap<String, PhpMixed>,
code: i64,
headers: Vec<String>,
body: Option<String>,
}
impl Response {
pub fn new(
request: IndexMap<String, PhpMixed>,
code: Option<i64>,
headers: Vec<String>,
body: Option<String>,
) -> anyhow::Result<Result<Self, LogicException>> {
if !request.contains_key("url") {
return Ok(Err(LogicException {
message: "url key missing from request array".to_string(),
code: 0,
}));
}
Ok(Ok(Self {
request,
code: code.unwrap_or(0),
headers,
body,
}))
}
pub fn get_status_code(&self) -> i64 {
self.code
}
pub fn get_status_message(&self) -> Option<String> {
let mut value = None;
for header in &self.headers {
if Preg::is_match(r"(?i)^HTTP/\S+ \d+", header).unwrap_or(false) {
// In case of redirects, headers contain the headers of all responses
// so we can not return directly and need to keep iterating
value = Some(header.clone());
}
}
value
}
pub fn get_headers(&self) -> &Vec<String> {
&self.headers
}
pub fn get_header(&self, name: &str) -> Option<String> {
Self::find_header_value(&self.headers, name)
}
pub fn get_body(&self) -> Option<&str> {
self.body.as_deref()
}
pub fn decode_json(&self) -> anyhow::Result<PhpMixed> {
let url = self
.request
.get("url")
.and_then(|u| u.as_string())
.unwrap_or("");
JsonFile::parse_json(self.body.as_deref(), Some(url))
}
pub fn collect(&mut self) {
self.request = IndexMap::new();
self.code = 0;
self.headers = vec![];
self.body = None;
}
pub fn find_header_value(headers: &[String], name: &str) -> Option<String> {
let mut value = None;
let pattern = format!("(?i)^{}:\\s*(.+?)\\s*$", preg_quote(name, None));
for header in headers {
let mut matches: indexmap::IndexMap<
shirabe_external_packages::composer::pcre::preg::CaptureKey,
String,
> = indexmap::IndexMap::new();
if Preg::match3(&pattern, header, Some(&mut matches)).unwrap_or(false) {
if let Some(s) = matches
.get(&shirabe_external_packages::composer::pcre::preg::CaptureKey::ByIndex(1))
{
value = Some(s.clone());
}
}
}
value
}
// TODO(phase-b): historical helpers used in composer_repository — provide stubs.
pub fn from_php_mixed(_data: PhpMixed) -> Self {
todo!()
}
pub fn to_php_mixed(&self) -> PhpMixed {
todo!()
}
pub fn new_fake(
_url: &str,
_code: i64,
_headers: IndexMap<String, PhpMixed>,
_body: String,
) -> Self {
todo!()
}
}
|