Debian Codegraph @

src/curl/HeaderlineParser.cpp

raw
1 /*
2 * this file is part of davix, the io library for http based protocols
3 * copyright (c) cern 2019
4 * author: georgios bitzes <[email protected]>
5 *
6 * this library is free software; you can redistribute it and/or
7 * modify it under the terms of the gnu lesser general public
8 * license as published by the free software foundation; either
9 * version 2.1 of the license, or (at your option) any later version.
10 *
11 * this library is distributed in the hope that it will be useful,
12 * but without any warranty; without even the implied warranty of
13 * merchantability or fitness for a particular purpose. see the gnu
14 * lesser general public license for more details.
15 *
16 * you should have received a copy of the gnu lesser general public
17 * license along with this library; if not, write to the free software
18 * foundation, inc., 51 franklin street, fifth floor, boston, ma 02110-1301 usa
19 *
20 */
21
22 #include "HeaderlineParser.hpp"
23
24 namespace Davix {
25
26 //------------------------------------------------------------------------------
27 // Constructor
28 //------------------------------------------------------------------------------
29 HeaderlineParser::HeaderlineParser(const char *buff, size_t len) {
30 parse(buff, len);
31 }
32
33 HeaderlineParser::HeaderlineParser(const std::string &str) {
34 parse(str.c_str(), str.size());
35 }
36
37 //------------------------------------------------------------------------------
38 // Main parse function
39 //------------------------------------------------------------------------------
40 void HeaderlineParser::parse(const char *buff, size_t len) {
41 if(buff == NULL || len == 0) {
42 return;
43 }
44
45 if(len >= 1 && buff[len-1] == '\0') {
46 len -= 1;
47 }
48
49 if(len >= 2 && buff[len-2] == '\r' && buff[len-1] == '\n') {
50 len -= 2;
51 }
52
53 size_t pos = 0;
54 while(pos < len) {
55 if(buff[pos] == ':') {
56 key = std::string(buff, pos);
57 pos++;
58 break;
59 }
60
61 pos++;
62 }
63
64 if(pos == len) {
65 // No ":", no key-value separation
66 key = std::string(buff, len);
67 return;
68 }
69
70 // skip leading value whitespace
71 while(pos < len && buff[pos] == ' ') {
72 pos++;
73 }
74
75 // consume value
76 value = std::string(buff+pos, len-pos);
77 }
78
79
80 //------------------------------------------------------------------------------
81 // Get key
82 //------------------------------------------------------------------------------
83 std::string HeaderlineParser::getKey() const {
84 return key;
85 }
86
87 //------------------------------------------------------------------------------
88 // Get value
89 //------------------------------------------------------------------------------
90 std::string HeaderlineParser::getValue() const {
91 return value;
92 }
93
94 }