1 | #include "requestheader.cpp"
|
---|
2 | #ifndef NETWORK_HEADERS
|
---|
3 | #include<NetAddress.h>
|
---|
4 | #include<NetBuffer.h>
|
---|
5 | #include<NetEndpoint.h>
|
---|
6 | #include<sys/socket.h>
|
---|
7 | #include<ByteOrder.h>
|
---|
8 | #include<netinet/in.h>
|
---|
9 | #include<arpa/inet.h>
|
---|
10 | #endif
|
---|
11 | //TODO
|
---|
12 | //1) Make provision for proxy less connections(direct connections)
|
---|
13 | //2) Eliminate hard coding asmuch as possible
|
---|
14 | //3) A response handler needs to be implemented.
|
---|
15 | class http{
|
---|
16 | BNetAddress *addr;
|
---|
17 | BNetEndpoint *ep;
|
---|
18 | public :
|
---|
19 | http();
|
---|
20 | http(string hostname , int port);
|
---|
21 | int request( httprequestheader& header);
|
---|
22 | int setHost(const string hostname ,int port =80);
|
---|
23 | int setProxy(string host ,int port ,string username,
|
---|
24 | string password);
|
---|
25 | bool connect();
|
---|
26 | };
|
---|
27 |
|
---|
28 | http::http(){}
|
---|
29 |
|
---|
30 | /*
|
---|
31 | For constructing a blank HTTP header
|
---|
32 | */
|
---|
33 | http::http(string hostname , int port){
|
---|
34 | //TODO
|
---|
35 | //Construction of blank HTTP header
|
---|
36 | }
|
---|
37 |
|
---|
38 | /*
|
---|
39 | Construct a new address
|
---|
40 | */
|
---|
41 | int
|
---|
42 | http::setProxy(string host ,int port ,string username ,string password){
|
---|
43 |
|
---|
44 | addr = new BNetAddress(inet_addr(host.c_str()),port);
|
---|
45 | if (addr->InitCheck()==B_OK)
|
---|
46 | cout<<"Fine";
|
---|
47 | }
|
---|
48 |
|
---|
49 | /*
|
---|
50 | Connect to a hardcoded proxy server.
|
---|
51 |
|
---|
52 | */
|
---|
53 | bool
|
---|
54 | http::connect(){
|
---|
55 | //TODO
|
---|
56 | //Accept values as parameters for proxy environment
|
---|
57 | ep = new BNetEndpoint(SOCK_STREAM);
|
---|
58 | BNetAddress obj(inet_addr("172.31.16.10"),8080);
|
---|
59 | if (ep->Connect(obj)==B_OK);
|
---|
60 | cout<"Connection open";
|
---|
61 |
|
---|
62 | }
|
---|
63 |
|
---|
64 | /*
|
---|
65 | Construct a request buffer and send it over the network
|
---|
66 | */
|
---|
67 | int
|
---|
68 | http::request( httprequestheader &header){
|
---|
69 | ep->Send(header.buffer , 0);
|
---|
70 | cout<<header.buffer;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /*
|
---|
74 | Main to test the code
|
---|
75 | */
|
---|
76 | int main(){
|
---|
77 | http httpsession;
|
---|
78 | httpsession.setProxy("172.31.16.10",8080,"","");
|
---|
79 | httprequestheader header("GET","http://www.rediff.com",
|
---|
80 | '1','1');
|
---|
81 | header.tostring();
|
---|
82 | httpsession.connect();
|
---|
83 | httpsession.request(header);
|
---|
84 | }
|
---|