1 | #define NETWORK_HEADERS
|
---|
2 | #include<NetBuffer.h>
|
---|
3 | #include<NetEndpoint.h>
|
---|
4 | #include<NetAddress.h>
|
---|
5 | #include<sys/socket.h>
|
---|
6 | #include<arpa/inet.h>
|
---|
7 | #include<netinet/in.h>
|
---|
8 | #include<ByteOrder.h>
|
---|
9 | #include<stdio.h>
|
---|
10 | #include<stdlib.h>
|
---|
11 | #include<iostream>
|
---|
12 | #include<string.h>
|
---|
13 | using namespace std;
|
---|
14 |
|
---|
15 | //Prototype class to represent a http header
|
---|
16 |
|
---|
17 | class haikuhttp{
|
---|
18 |
|
---|
19 | public:
|
---|
20 | BNetBuffer *buffer;
|
---|
21 | const void *CRLF;
|
---|
22 | haikuhttp();
|
---|
23 | haikuhttp(haikuhttp &header);
|
---|
24 | bool addValue(const char *key ,const char* value);
|
---|
25 | string getallValues();
|
---|
26 | void removeAllValues(string key);
|
---|
27 | void removeValue(string key);
|
---|
28 | void setContentLength(int len);
|
---|
29 | void setContentType(string type);
|
---|
30 | bool hasContentLength();
|
---|
31 | bool hasKey(string key);
|
---|
32 | void setValue(const char* key , const char* value);
|
---|
33 | const char* value(string key);
|
---|
34 | void tostring() const;
|
---|
35 | bool isValidHeader(const char* header) const;
|
---|
36 |
|
---|
37 | };
|
---|
38 |
|
---|
39 | //Construct an empty http header for latter additions to the buffer
|
---|
40 | haikuhttp::haikuhttp(){
|
---|
41 | buffer = new BNetBuffer(1024);
|
---|
42 | CRLF="\r\n";
|
---|
43 | }
|
---|
44 |
|
---|
45 | bool
|
---|
46 | haikuhttp::isValidHeader(const char *header) const{
|
---|
47 | //TODO
|
---|
48 | //a C++ interators use would be more efficient
|
---|
49 | const string valid_headers[]={"Accept","Accept-Charset","Accept-Encoding","Accept-Language",
|
---|
50 | "Authorization","Expect","From","Host","If-Match,If-Modified-Since",
|
---|
51 | "If-None-Match","If-Range","If-Unmodified-Since","Max-Forwards","Proxy-Authorization",
|
---|
52 | "Range","Referer","TE","Keep-Alive","User-Agent"};
|
---|
53 | int i;
|
---|
54 | for ( i =0 ; i<20; i++) {
|
---|
55 | if (strcmp(valid_headers[i].c_str(),header)==0)
|
---|
56 | return true;
|
---|
57 | }
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /*
|
---|
62 | Set values for the valid keys and add to header buffer area
|
---|
63 | Add values to the buffer aread
|
---|
64 | */
|
---|
65 | bool
|
---|
66 | haikuhttp::addValue(const char* header , const char* value){
|
---|
67 | if ( isValidHeader(header)){
|
---|
68 | buffer->AppendData((const void *)header , strlen(header));
|
---|
69 | buffer->AppendData((const void *)":",1);
|
---|
70 | buffer->AppendData((const void *)value , strlen(value));
|
---|
71 | //Every header should terminate in CRLF
|
---|
72 | buffer->AppendData(CRLF , 2);
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 | cout<<"Invalid headers";
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 | /*
|
---|
79 | If he content length feild is found in the buffer
|
---|
80 | returns true
|
---|
81 | */
|
---|
82 | bool
|
---|
83 | haikuhttp::hasContentLength(){
|
---|
84 | if (strstr("Content-Length",(const char *)buffer->Data())==NULL)
|
---|
85 | return false;
|
---|
86 | else
|
---|
87 | return true;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /*
|
---|
91 | Returns true if a particular key is found in the header
|
---|
92 | */
|
---|
93 | bool
|
---|
94 | haikuhttp::hasKey(string key){
|
---|
95 | if (strstr(key.c_str(),(const char *)buffer->Data())!=NULL)
|
---|
96 | return true;
|
---|
97 | else
|
---|
98 | return false;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /*
|
---|
102 | Returns the first value for the entry with the given key
|
---|
103 | If no entry has this key, and empty string is returned.
|
---|
104 | */
|
---|
105 | const char*
|
---|
106 | haikuhttp::value(string key){
|
---|
107 | char *ptr,*str,*tempstart;
|
---|
108 | int start , end , len;
|
---|
109 | ptr = strstr((const char *)buffer->Data(),key.c_str());
|
---|
110 | str=(char*)malloc(10);
|
---|
111 | while (*ptr==' ')
|
---|
112 | ptr++;
|
---|
113 | while (*ptr!=':')
|
---|
114 | //Reach the : first
|
---|
115 | ptr++;
|
---|
116 | ptr++;
|
---|
117 | while (*ptr==' ')
|
---|
118 | ptr++;
|
---|
119 | while (*ptr!=','&&*ptr!='\r'){
|
---|
120 | cout<<*ptr;
|
---|
121 | *str=*ptr ;
|
---|
122 | ptr++;
|
---|
123 | str++;
|
---|
124 | }
|
---|
125 | str='\0';
|
---|
126 | return str;
|
---|
127 | }
|
---|
128 | /*
|
---|
129 | Prints the header
|
---|
130 | */
|
---|
131 | //TODO
|
---|
132 | //Should return a string
|
---|
133 |
|
---|
134 | void
|
---|
135 | haikuhttp::tostring() const{
|
---|
136 | cout<<"Buffer Info";
|
---|
137 | printf("%s",buffer->Data());
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | /*Test main to test the buffer contents
|
---|
142 | This is the test main which can be used to
|
---|
143 | test the class.
|
---|
144 | */
|
---|
145 | /*
|
---|
146 | int
|
---|
147 | main()
|
---|
148 | {
|
---|
149 | haikuhttp httpobj;
|
---|
150 | cout<<"object creation success";
|
---|
151 | httpobj.addValue("Accept-Language","en-us,en");
|
---|
152 | httpobj.addValue("Accept-Encoding","gzip,deflate");
|
---|
153 | httpobj.addValue("Keep-Alive","300");
|
---|
154 |
|
---|
155 | cout<<"\nValue output\n";
|
---|
156 | httpobj.value("Accept-Encoding");
|
---|
157 | cout<<"AcceptLange: ";
|
---|
158 | cout<<httpobj.value("Accept-Language");
|
---|
159 | cout<<"\n";
|
---|
160 | cout<<"\n----------------\n";
|
---|
161 | cout<<"header information";
|
---|
162 | httpobj.tostring();
|
---|
163 | return 0;
|
---|
164 | }*/
|
---|
165 |
|
---|