Ticket #5677: networkbase.cpp

File networkbase.cpp, 3.6 KB (added by rohitvvv@…, 14 years ago)
Line 
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>
13using namespace std;
14
15//Prototype class to represent a http header
16
17class 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
40haikuhttp::haikuhttp(){
41 buffer = new BNetBuffer(1024);
42 CRLF="\r\n";
43}
44
45bool
46haikuhttp::isValidHeader(const char *header) const{
47//TODO
48//a C++ interators use would be more efficient
49const 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 }
58return false;
59}
60
61/*
62Set values for the valid keys and add to header buffer area
63Add values to the buffer aread
64*/
65bool
66haikuhttp::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 }
75cout<<"Invalid headers";
76return false;
77}
78/*
79If he content length feild is found in the buffer
80returns true
81*/
82bool
83haikuhttp::hasContentLength(){
84 if (strstr("Content-Length",(const char *)buffer->Data())==NULL)
85 return false;
86 else
87 return true;
88}
89
90/*
91Returns true if a particular key is found in the header
92*/
93bool
94haikuhttp::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/*
102Returns the first value for the entry with the given key
103If no entry has this key, and empty string is returned.
104*/
105const char*
106haikuhttp::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/*
129Prints the header
130*/
131//TODO
132//Should return a string
133
134void
135haikuhttp::tostring() const{
136 cout<<"Buffer Info";
137 printf("%s",buffer->Data());
138}
139
140
141/*Test main to test the buffer contents
142This is the test main which can be used to
143test the class.
144*/
145/*
146int
147main()
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