|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: ?Description |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "nativehttpsconnection.h" |
|
20 #include <stdio.h> |
|
21 #include<errno.h> |
|
22 #include<fcntl.h> |
|
23 #include<unistd.h> |
|
24 #include<sys/stat.h> |
|
25 #include<sys/types.h> |
|
26 #include <stdlib.h> |
|
27 #include <sys/socket.h> |
|
28 #include <netinet/in.h> |
|
29 #include <netdb.h> |
|
30 #include <unistd.h> |
|
31 #include <arpa/inet.h> |
|
32 #include <sstream> |
|
33 #include <string.h> |
|
34 |
|
35 #include <logger.h> |
|
36 |
|
37 namespace java |
|
38 { |
|
39 namespace https |
|
40 { |
|
41 int NativeHttpsConnection::createSocket(const char *aProxy,int aProxyPort, const char *aHost, int aPort) |
|
42 { |
|
43 JELOG2(ESOCKET); |
|
44 PLOG4(ESOCKET, "Creating socket, proxy host = %s, proxy port = %d, host = %s, port = %d ", |
|
45 aProxy, aProxyPort, aHost, aPort); |
|
46 |
|
47 //Create TCP socket to proxy server |
|
48 |
|
49 struct sockaddr_in addr; |
|
50 addr.sin_family = AF_INET; |
|
51 addr.sin_port = htons(aProxyPort); |
|
52 |
|
53 if (!inet_aton(aProxy, &addr.sin_addr)) |
|
54 { |
|
55 struct hostent* hp = gethostbyname(aProxy); |
|
56 addr.sin_addr.s_addr = ((struct in_addr*)(hp->h_addr))->s_addr; |
|
57 } |
|
58 |
|
59 int sd = socket(AF_INET, SOCK_STREAM, 0); |
|
60 LOG1(ESOCKET, EInfo, "fd = %d", sd); |
|
61 int rb = bind(sd, (struct sockaddr*) &addr, sizeof(addr)); |
|
62 int rc = connect(sd, (struct sockaddr*) &addr, sizeof(addr)); |
|
63 |
|
64 if (rc < 0) |
|
65 return -errno; // could not connect to proxy server, return |
|
66 |
|
67 std::string string_host = aHost; |
|
68 LOG1(ESOCKET, EInfo, "ahost = %s", aHost); |
|
69 std::ostringstream tmpbuf; |
|
70 tmpbuf << aPort; |
|
71 std::string string_port = tmpbuf.str(); |
|
72 |
|
73 std::string tmpst1 = "CONNECT " + string_host + ":"; |
|
74 tmpst1 = tmpst1 + string_port; |
|
75 tmpst1 = tmpst1 + " HTTP/1.1\r\n\r\n"; |
|
76 char *p1 = (char *) tmpst1.c_str(); |
|
77 |
|
78 // Example p1 ="CONNECT www.google.com:443 HTTP/1.1\r\n\r\n" |
|
79 |
|
80 int res = send(sd, p1, strlen(p1), 0); |
|
81 |
|
82 if (res < 0) |
|
83 return -errno; |
|
84 |
|
85 LOG1(ESOCKET, EInfo, "HttpsConnection() tmp string %s", p1); |
|
86 LOG1(ESOCKET, EInfo, "send returned %d", res); |
|
87 LOG1(ESOCKET, EInfo, "NativeHttpsConnection::receiving using sd %d", sd); |
|
88 char response[1024]; |
|
89 int rcnt = recv(sd, response, 1024, 0); |
|
90 LOG1(ESOCKET, EInfo, "recv returned %d", rcnt); |
|
91 response[rcnt] = 0; // null termination |
|
92 for (int i = 0; i < rcnt; i++) |
|
93 LOG1(ESOCKET, EInfo, "%c", response[i]); |
|
94 |
|
95 // for CONNECT request, the response line will be of the form "HTTP/1.1 200 Connection established\r\n" |
|
96 // check if the response code is 200, if it is not 200 then this website cannot be accessed via this proxy, so return error code |
|
97 |
|
98 std::string s3 = response; |
|
99 std::string pattern = "200"; |
|
100 size_t found = s3.find(pattern); |
|
101 |
|
102 if (found != std::string::npos) |
|
103 { |
|
104 LOG1(ESOCKET, EInfo, "Proxy returned 200 Connection established response", 2); |
|
105 return sd; |
|
106 } |
|
107 else |
|
108 return -1; // error code |
|
109 |
|
110 |
|
111 } |
|
112 } // end namespace https |
|
113 |
|
114 } // end namespace java |