gurtlib 0.1.0

Official GURT:// protocol implementation
Documentation
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use crate::{GurtError, Result, GURT_VERSION};
use crate::protocol::{GurtStatusCode, PROTOCOL_PREFIX, HEADER_SEPARATOR, BODY_SEPARATOR};
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
use std::fmt;
use chrono::Utc;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum GurtMethod {
    GET,
    POST,
    PUT,
    DELETE,
    HEAD,
    OPTIONS,
    PATCH,
    HANDSHAKE,
}

impl GurtMethod {
    pub fn parse(s: &str) -> Result<Self> {
        match s.to_uppercase().as_str() {
            "GET" => Ok(Self::GET),
            "POST" => Ok(Self::POST),
            "PUT" => Ok(Self::PUT),
            "DELETE" => Ok(Self::DELETE),
            "HEAD" => Ok(Self::HEAD),
            "OPTIONS" => Ok(Self::OPTIONS),
            "PATCH" => Ok(Self::PATCH),
            "HANDSHAKE" => Ok(Self::HANDSHAKE),
            _ => Err(GurtError::invalid_message(format!("Unsupported method: {}", s))),
        }
    }
}

impl fmt::Display for GurtMethod {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            Self::GET => "GET",
            Self::POST => "POST",
            Self::PUT => "PUT",
            Self::DELETE => "DELETE",
            Self::HEAD => "HEAD",
            Self::OPTIONS => "OPTIONS",
            Self::PATCH => "PATCH",
            Self::HANDSHAKE => "HANDSHAKE",
        };
        write!(f, "{}", s)
    }
}

pub type GurtHeaders = HashMap<String, String>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GurtRequest {
    pub method: GurtMethod,
    pub path: String,
    pub version: String,
    pub headers: GurtHeaders,
    pub body: Vec<u8>,
}

impl GurtRequest {
    pub fn new(method: GurtMethod, path: String) -> Self {
        Self {
            method,
            path,
            version: GURT_VERSION.to_string(),
            headers: GurtHeaders::new(),
            body: Vec::new(),
        }
    }
    
    pub fn with_header<K: Into<String>, V: Into<String>>(mut self, key: K, value: V) -> Self {
        self.headers.insert(key.into().to_lowercase(), value.into());
        self
    }
    
    pub fn with_body<B: Into<Vec<u8>>>(mut self, body: B) -> Self {
        self.body = body.into();
        self
    }
    
    pub fn with_string_body<S: AsRef<str>>(mut self, body: S) -> Self {
        self.body = body.as_ref().as_bytes().to_vec();
        self
    }
    
    pub fn header(&self, key: &str) -> Option<&String> {
        self.headers.get(&key.to_lowercase())
    }
    
    pub fn text(&self) -> Result<String> {
        std::str::from_utf8(&self.body)
            .map(|s| s.to_string())
            .map_err(|e| GurtError::invalid_message(format!("Invalid UTF-8 body: {}", e)))
    }
    
    pub fn parse(data: &str) -> Result<Self> {
        Self::parse_bytes(data.as_bytes())
    }
    
    pub fn parse_bytes(data: &[u8]) -> Result<Self> {
        let body_separator = BODY_SEPARATOR.as_bytes();
        let body_separator_pos = data.windows(body_separator.len())
            .position(|window| window == body_separator);
        
        let (headers_section, body) = if let Some(pos) = body_separator_pos {
            let headers_part = &data[..pos];
            let body_part = &data[pos + body_separator.len()..];
            (headers_part, body_part.to_vec())
        } else {
            (data, Vec::new())
        };
        
        let headers_str = std::str::from_utf8(headers_section)
            .map_err(|_| GurtError::invalid_message("Invalid UTF-8 in headers"))?;
        
        let lines: Vec<&str> = headers_str.split(HEADER_SEPARATOR).collect();
        
        if lines.is_empty() {
            return Err(GurtError::invalid_message("Empty request"));
        }
        
        let request_line = lines[0];
        let parts: Vec<&str> = request_line.split_whitespace().collect();
        
        if parts.len() != 3 {
            return Err(GurtError::invalid_message("Invalid request line format"));
        }
        
        let method = GurtMethod::parse(parts[0])?;
        let path = parts[1].to_string();
        
        if !parts[2].starts_with(PROTOCOL_PREFIX) {
            return Err(GurtError::invalid_message("Invalid protocol identifier"));
        }
        
        let version_str = &parts[2][PROTOCOL_PREFIX.len()..];
        let version = version_str.to_string();
        
        let mut headers = GurtHeaders::new();
        
        for line in lines.iter().skip(1) {
            if line.is_empty() {
                break;
            }
            
            if let Some(colon_pos) = line.find(':') {
                let key = line[..colon_pos].trim().to_lowercase();
                let value = line[colon_pos + 1..].trim().to_string();
                headers.insert(key, value);
            }
        }
        
        Ok(Self {
            method,
            path,
            version,
            headers,
            body,
        })
    }
    
    pub fn to_string(&self) -> String {
        let mut message = format!("{} {} {}{}{}", 
            self.method, self.path, PROTOCOL_PREFIX, self.version, HEADER_SEPARATOR);
        
        let mut headers = self.headers.clone();
        if !headers.contains_key("content-length") {
            headers.insert("content-length".to_string(), self.body.len().to_string());
        }
        
        if !headers.contains_key("user-agent") {
            headers.insert("user-agent".to_string(), format!("GURT-Client/{}", GURT_VERSION));
        }
        
        for (key, value) in &headers {
            message.push_str(&format!("{}: {}{}", key, value, HEADER_SEPARATOR));
        }
        
        message.push_str(HEADER_SEPARATOR);
        
        if !self.body.is_empty() {
            if let Ok(body_str) = std::str::from_utf8(&self.body) {
                message.push_str(body_str);
            }
        }
        
        message
    }
    
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut message = format!("{} {} {}{}{}", 
            self.method, self.path, PROTOCOL_PREFIX, self.version, HEADER_SEPARATOR);
        
        let mut headers = self.headers.clone();
        if !headers.contains_key("content-length") {
            headers.insert("content-length".to_string(), self.body.len().to_string());
        }
        
        if !headers.contains_key("user-agent") {
            headers.insert("user-agent".to_string(), format!("GURT-Client/{}", GURT_VERSION));
        }
        
        for (key, value) in &headers {
            message.push_str(&format!("{}: {}{}", key, value, HEADER_SEPARATOR));
        }
        
        message.push_str(HEADER_SEPARATOR);
        
        let mut bytes = message.into_bytes();
        bytes.extend_from_slice(&self.body);
        
        bytes
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GurtResponse {
    pub version: String,
    pub status_code: u16,
    pub status_message: String,
    pub headers: GurtHeaders,
    pub body: Vec<u8>,
}

impl GurtResponse {
    pub fn new(status_code: GurtStatusCode) -> Self {
        Self {
            version: GURT_VERSION.to_string(),
            status_code: status_code as u16,
            status_message: status_code.message().to_string(),
            headers: GurtHeaders::new(),
            body: Vec::new(),
        }
    }
    
    pub fn ok() -> Self {
        Self::new(GurtStatusCode::Ok)
    }
    
    pub fn not_found() -> Self {
        Self::new(GurtStatusCode::NotFound)
    }
    
    pub fn bad_request() -> Self {
        Self::new(GurtStatusCode::BadRequest)
    }
    
    pub fn forbidden() -> Self {
        Self::new(GurtStatusCode::Forbidden)
    }
    
    pub fn internal_server_error() -> Self {
        Self::new(GurtStatusCode::InternalServerError)
    }
    
    pub fn with_header<K: Into<String>, V: Into<String>>(mut self, key: K, value: V) -> Self {
        self.headers.insert(key.into().to_lowercase(), value.into());
        self
    }
    
    pub fn with_body<B: Into<Vec<u8>>>(mut self, body: B) -> Self {
        self.body = body.into();
        self
    }
    
    pub fn with_string_body<S: AsRef<str>>(mut self, body: S) -> Self {
        self.body = body.as_ref().as_bytes().to_vec();
        self
    }
    
    pub fn with_json_body<T: Serialize>(mut self, data: &T) -> Result<Self> {
        let json = serde_json::to_string(data)?;
        self.body = json.into_bytes();
        self.headers.insert("content-type".to_string(), "application/json".to_string());
        Ok(self)
    }
    
    pub fn header(&self, key: &str) -> Option<&String> {
        self.headers.get(&key.to_lowercase())
    }
    
    pub fn text(&self) -> Result<String> {
        std::str::from_utf8(&self.body)
            .map(|s| s.to_owned())
            .map_err(|e| GurtError::invalid_message(format!("Invalid UTF-8 body: {}", e)))
    }
    
    pub fn is_success(&self) -> bool {
        self.status_code >= 200 && self.status_code < 300
    }
    
    pub fn is_client_error(&self) -> bool {
        self.status_code >= 400 && self.status_code < 500
    }
    
    pub fn is_server_error(&self) -> bool {
        self.status_code >= 500
    }
    
    pub fn parse(data: &str) -> Result<Self> {
        Self::parse_bytes(data.as_bytes())
    }
    
    pub fn parse_bytes(data: &[u8]) -> Result<Self> {
        let body_separator = BODY_SEPARATOR.as_bytes();
        let body_separator_pos = data.windows(body_separator.len())
            .position(|window| window == body_separator);
        
        let (headers_section, body) = if let Some(pos) = body_separator_pos {
            let headers_part = &data[..pos];
            let body_part = &data[pos + body_separator.len()..];
            (headers_part, body_part.to_vec())
        } else {
            (data, Vec::new())
        };
        
        let headers_str = std::str::from_utf8(headers_section)
            .map_err(|_| GurtError::invalid_message("Invalid UTF-8 in headers"))?;
        
        let lines: Vec<&str> = headers_str.split(HEADER_SEPARATOR).collect();
        
        if lines.is_empty() {
            return Err(GurtError::invalid_message("Empty response"));
        }
        
        let status_line = lines[0];
        let parts: Vec<&str> = status_line.splitn(3, ' ').collect();
        
        if parts.len() < 2 {
            return Err(GurtError::invalid_message("Invalid status line format"));
        }
        
        if !parts[0].starts_with(PROTOCOL_PREFIX) {
            return Err(GurtError::invalid_message("Invalid protocol identifier"));
        }
        
        let version_str = &parts[0][PROTOCOL_PREFIX.len()..];
        let version = version_str.to_string();
        
        let status_code: u16 = parts[1].parse()
            .map_err(|_| GurtError::invalid_message("Invalid status code"))?;
        
        let status_message = if parts.len() > 2 {
            parts[2].to_string()
        } else {
            GurtStatusCode::from_u16(status_code)
                .map(|sc| sc.message().to_string())
                .unwrap_or_else(|| "Unknown".to_string())
        };
        
        let mut headers = GurtHeaders::new();
        
        for line in lines.iter().skip(1) {
            if line.is_empty() {
                break;
            }
            
            if let Some(colon_pos) = line.find(':') {
                let key = line[..colon_pos].trim().to_lowercase();
                let value = line[colon_pos + 1..].trim().to_string();
                headers.insert(key, value);
            }
        }
        
        Ok(Self {
            version,
            status_code,
            status_message,
            headers,
            body,
        })
    }
    
    pub fn to_string(&self) -> String {
        let mut message = format!("{}{} {} {}{}", 
            PROTOCOL_PREFIX, self.version, self.status_code, self.status_message, HEADER_SEPARATOR);
        
        let mut headers = self.headers.clone();
        if !headers.contains_key("content-length") {
            headers.insert("content-length".to_string(), self.body.len().to_string());
        }
        
        if !headers.contains_key("server") {
            headers.insert("server".to_string(), format!("GURT/{}", GURT_VERSION));
        }
        
        if !headers.contains_key("date") {
            let now = Utc::now();
            let date_str = now.format("%a, %d %b %Y %H:%M:%S GMT").to_string();
            headers.insert("date".to_string(), date_str);
        }
        
        for (key, value) in &headers {
            message.push_str(&format!("{}: {}{}", key, value, HEADER_SEPARATOR));
        }
        
        message.push_str(HEADER_SEPARATOR);
        
        if !self.body.is_empty() {
            if let Ok(body_str) = std::str::from_utf8(&self.body) {
                message.push_str(body_str);
            }
        }
        
        message
    }
    
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut message = format!("{}{} {} {}{}", 
            PROTOCOL_PREFIX, self.version, self.status_code, self.status_message, HEADER_SEPARATOR);
        
        let mut headers = self.headers.clone();
        if !headers.contains_key("content-length") {
            headers.insert("content-length".to_string(), self.body.len().to_string());
        }
        
        if !headers.contains_key("server") {
            headers.insert("server".to_string(), format!("GURT/{}", GURT_VERSION));
        }
        
        if !headers.contains_key("date") {
            let now = Utc::now();
            let date_str = now.format("%a, %d %b %Y %H:%M:%S GMT").to_string();
            headers.insert("date".to_string(), date_str);
        }
        
        for (key, value) in &headers {
            message.push_str(&format!("{}: {}{}", key, value, HEADER_SEPARATOR));
        }
        
        message.push_str(HEADER_SEPARATOR);
        
        let mut bytes = message.into_bytes();
        bytes.extend_from_slice(&self.body);
        
        bytes
    }
}

#[derive(Debug, Clone)]
pub enum GurtMessage {
    Request(GurtRequest),
    Response(GurtResponse),
}

impl GurtMessage {
    pub fn parse(data: &str) -> Result<Self> {
        Self::parse_bytes(data.as_bytes())
    }
    
    pub fn parse_bytes(data: &[u8]) -> Result<Self> {
        let header_separator = HEADER_SEPARATOR.as_bytes();
        let first_line_end = data.windows(header_separator.len())
            .position(|window| window == header_separator)
            .unwrap_or(data.len());
        
        let first_line = std::str::from_utf8(&data[..first_line_end])
            .map_err(|_| GurtError::invalid_message("Invalid UTF-8 in first line"))?;
        
        if first_line.starts_with(PROTOCOL_PREFIX) {
            Ok(GurtMessage::Response(GurtResponse::parse_bytes(data)?))
        } else {
            Ok(GurtMessage::Request(GurtRequest::parse_bytes(data)?))
        }
    }
    
    pub fn is_request(&self) -> bool {
        matches!(self, GurtMessage::Request(_))
    }
    
    pub fn is_response(&self) -> bool {
        matches!(self, GurtMessage::Response(_))
    }
    
    pub fn as_request(&self) -> Option<&GurtRequest> {
        match self {
            GurtMessage::Request(req) => Some(req),
            _ => None,
        }
    }
    
    pub fn as_response(&self) -> Option<&GurtResponse> {
        match self {
            GurtMessage::Response(res) => Some(res),
            _ => None,
        }
    }
}

impl fmt::Display for GurtMessage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            GurtMessage::Request(req) => write!(f, "{}", req.to_string()),
            GurtMessage::Response(res) => write!(f, "{}", res.to_string()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_request_parsing() {
        let raw = "GET /test GURT/1.0.0\r\nHost: example.com\r\nAccept: text/html\r\n\r\ntest body";
        let request = GurtRequest::parse(raw).expect("Failed to parse request");
        
        assert_eq!(request.method, GurtMethod::GET);
        assert_eq!(request.path, "/test");
        assert_eq!(request.version, GURT_VERSION.to_string());
        assert_eq!(request.header("host"), Some(&"example.com".to_string()));
        assert_eq!(request.header("accept"), Some(&"text/html".to_string()));
        assert_eq!(request.text().unwrap(), "test body");
    }
    
    #[test]
    fn test_response_parsing() {
        let raw = "GURT/1.0.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html></html>";
        let response = GurtResponse::parse(raw).expect("Failed to parse response");
        
        assert_eq!(response.version, GURT_VERSION.to_string());
        assert_eq!(response.status_code, 200);
        assert_eq!(response.status_message, "OK");
        assert_eq!(response.header("content-type"), Some(&"text/html".to_string()));
        assert_eq!(response.text().unwrap(), "<html></html>");
    }
    
    #[test]
    fn test_request_building() {
        let request = GurtRequest::new(GurtMethod::GET, "/test".to_string())
            .with_header("Host", "example.com")
            .with_string_body("test body");
        
        let raw = request.to_string();
        let parsed = GurtRequest::parse(&raw).expect("Failed to parse built request");
        
        assert_eq!(parsed.method, request.method);
        assert_eq!(parsed.path, request.path);
        assert_eq!(parsed.body, request.body);
    }
    
    #[test]
    fn test_response_building() {
        let response = GurtResponse::ok()
            .with_header("Content-Type", "text/html")
            .with_string_body("<html></html>");
        
        let raw = response.to_string();
        let parsed = GurtResponse::parse(&raw).expect("Failed to parse built response");
        
        assert_eq!(parsed.status_code, response.status_code);
        assert_eq!(parsed.body, response.body);
    }
}