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
use bytes::Bytes;
use vector_lib::lookup::path;
use vector_lib::{
    config::{LegacyKey, LogNamespace},
    event::Event,
};
use warp::http::{HeaderMap, HeaderValue};

use crate::event::Value;
use crate::sources::http_server::HttpConfigParamKind;

pub fn add_headers(
    events: &mut [Event],
    headers_config: &[HttpConfigParamKind],
    headers: &HeaderMap,
    log_namespace: LogNamespace,
    source_name: &'static str,
) {
    for h in headers_config {
        match h {
            // Add each non-wildcard containing header that was specified
            // in the `headers` config option to the event if an exact match
            // is found.
            HttpConfigParamKind::Exact(header_name) => {
                let value = headers.get(header_name).map(HeaderValue::as_bytes);

                for event in events.iter_mut() {
                    if let Event::Log(log) = event {
                        log_namespace.insert_source_metadata(
                            source_name,
                            log,
                            Some(LegacyKey::InsertIfEmpty(path!(header_name))),
                            path!("headers", header_name),
                            Value::from(value.map(Bytes::copy_from_slice)),
                        );
                    }
                }
            }
            // Add all headers that match against wildcard pattens specified
            // in the `headers` config option to the event.
            HttpConfigParamKind::Glob(header_pattern) => {
                for header_name in headers.keys() {
                    if header_pattern
                        .matches_with(header_name.as_str(), glob::MatchOptions::default())
                    {
                        let value = headers.get(header_name).map(HeaderValue::as_bytes);

                        for event in events.iter_mut() {
                            if let Event::Log(log) = event {
                                log_namespace.insert_source_metadata(
                                    source_name,
                                    log,
                                    Some(LegacyKey::InsertIfEmpty(path!(header_name.as_str()))),
                                    path!("headers", header_name.as_str()),
                                    Value::from(value.map(Bytes::copy_from_slice)),
                                );
                            }
                        }
                    }
                }
            }
        };
    }
}

#[cfg(test)]
mod tests {
    use crate::event::LogEvent;
    use crate::sources::{http_server::HttpConfigParamKind, util::add_headers};

    use vector_lib::config::LogNamespace;
    use vrl::{path, value};
    use warp::http::HeaderMap;

    #[test]
    fn multiple_headers() {
        let header_names = [
            HttpConfigParamKind::Exact("Content-Type".into()),
            HttpConfigParamKind::Exact("User-Agent".into()),
        ];
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/x-protobuf".parse().unwrap());
        headers.insert("User-Agent", "Test".parse().unwrap());
        headers.insert("Content-Encoding", "gzip".parse().unwrap());

        let mut base_log = [LogEvent::from(value!({})).into()];
        add_headers(
            &mut base_log,
            &header_names,
            &headers,
            LogNamespace::Legacy,
            "test",
        );
        let mut namespaced_log = [LogEvent::from(value!({})).into()];
        add_headers(
            &mut namespaced_log,
            &header_names,
            &headers,
            LogNamespace::Vector,
            "test",
        );

        assert_eq!(
            base_log[0].as_log().value(),
            namespaced_log[0]
                .metadata()
                .value()
                .get(path!("test", "headers"))
                .unwrap()
        );
    }

    #[test]
    fn multiple_headers_wildcard() {
        let header_names = [HttpConfigParamKind::Glob(
            glob::Pattern::new("Content-*").unwrap(),
        )];
        let mut headers = HeaderMap::new();
        headers.insert("Content-Type", "application/x-protobuf".parse().unwrap());
        headers.insert("User-Agent", "Test".parse().unwrap());
        headers.insert("Content-Encoding", "gzip".parse().unwrap());

        let mut base_log = [LogEvent::from(value!({})).into()];
        add_headers(
            &mut base_log,
            &header_names,
            &headers,
            LogNamespace::Legacy,
            "test",
        );
        let mut namespaced_log = [LogEvent::from(value!({})).into()];
        add_headers(
            &mut namespaced_log,
            &header_names,
            &headers,
            LogNamespace::Vector,
            "test",
        );

        let log = base_log[0].as_log();
        assert_eq!(
            log.value(),
            namespaced_log[0]
                .metadata()
                .value()
                .get(path!("test", "headers"))
                .unwrap(),
            "Checking legacy and namespaced log contain headers string"
        );
        assert_eq!(
            log["content-type"],
            "application/x-protobuf".into(),
            "Checking log contains Content-Type header"
        );
        assert!(
            !log.contains("user-agent"),
            "Checking log does not contain User-Agent header"
        );
        assert_eq!(
            log["content-encoding"],
            "gzip".into(),
            "Checking log contains Content-Encoding header"
        );
    }
}