3333
3434// ============================ PRIVATE FUNCTIONS ============================
3535
36- int createTcpSocket (void ){
36+ int HttpPost:: createTcpSocket (void ){
3737 /* Build the socket. */
3838 struct protoent * protoent = getprotobyname (" tcp" );
3939 if (protoent == NULL ) {
4040 perror (" getprotobyname" );
4141 return -1 ;
4242 }
43- int tcpSocket = socket (AF_INET, SOCK_STREAM, protoent->p_proto );
43+ tcpSocket = socket (AF_INET, SOCK_STREAM, protoent->p_proto );
4444 if (tcpSocket == -1 ) {
4545 perror (" socket" );
4646 return -1 ;
4747 }
48+ return 0 ;
4849}
4950
50- int connectTcp (const int &socket, const std::wstring &url, const std::wstring &port){
51+ int HttpPost:: connectTcp (const std::wstring &url, const std::wstring &port){
5152 /* Build the address. */
5253 struct hostent *hostent = gethostbyname (ws2s (url).c_str ());
5354 if (hostent == NULL ) {
@@ -66,14 +67,14 @@ int connectTcp(const int &socket, const std::wstring &url, const std::wstring &p
6667 sockaddr_in.sin_port = htons (server_port);
6768
6869 /* connect. */
69- if (connect (socket , (struct sockaddr *)&sockaddr_in, sizeof (sockaddr_in)) == -1 ) {
70+ if (connect (tcpSocket , (struct sockaddr *)&sockaddr_in, sizeof (sockaddr_in)) == -1 ) {
7071 // perror("connect");
71- close (socket );
72+ close (tcpSocket );
7273 return -2 ;
7374 }
7475}
7576
76- int sendHttpRequest (const int &socket, const std::wstring &request){
77+ int HttpPost:: sendHttpRequest (const std::wstring &request){
7778 ssize_t nbytes_total;
7879 ssize_t nbytes_last;
7980 size_t request_len = request.length ();
@@ -84,44 +85,44 @@ int sendHttpRequest(const int &socket, const std::wstring &request){
8485 std::wstring wideChunk = request.substr (nbytes_total);
8586 std::string narrowChunk = ws2s (wideChunk);
8687 size_t narrowBytesToSend = narrowChunk.length ();
87- int nbytes_last = write (socket , narrowChunk.c_str (), static_cast <int >(narrowBytesToSend));
88+ int nbytes_last = write (tcpSocket , narrowChunk.c_str (), static_cast <int >(narrowBytesToSend));
8889 if (nbytes_last == -1 ) {
89- close (socket );
90+ close (tcpSocket );
9091 return -1 ;
9192 // return std::wstring();
9293 }
9394 nbytes_total += nbytes_last; // Increment by the number of bytes sent
9495 }
9596}
9697
97- std::wstring recvHttpResponse (const int &socket ){
98+ std::wstring HttpPost:: recvHttpResponse (void ){
9899 /* Read the response with timeout. */
99100 char readBuff[READ_BUFFER_SIZE] = {};
100101 ssize_t nbytes=0 ;
101102
102103 fd_set read_fds;
103104 FD_ZERO (&read_fds);
104- FD_SET (socket , &read_fds);
105+ FD_SET (tcpSocket , &read_fds);
105106
106107 struct timeval timeout;
107108 timeout.tv_sec = 0 ;
108109 timeout.tv_usec = 500 * 1000 ; // 500 miliseconds
109110
110111 std::string tmpDataRead;
111- int select_result = select (socket + 1 , &read_fds, NULL , NULL , &timeout);
112+ int select_result = select (tcpSocket + 1 , &read_fds, NULL , NULL , &timeout);
112113 if (select_result == -1 ) {
113114 // perror("select");
114- close (socket );
115+ close (tcpSocket );
115116 return std::wstring ();
116117 }
117118 else if (select_result == 0 ) {
118- // Timeout occurred, no data received within 2 seconds
119+ // Timeout occurred, if no data received within 2 seconds
119120 // std::cout << "Timeout occurred. No data received within 2 seconds." << std::endl;
120- close (socket );
121+ close (tcpSocket );
121122 return std::wstring ();
122123 }
123- if (FD_ISSET (socket , &read_fds)) {
124- while ((nbytes = read (socket , readBuff, READ_BUFFER_SIZE)) > 0 ) {
124+ if (FD_ISSET (tcpSocket , &read_fds)) {
125+ while ((nbytes = read (tcpSocket , readBuff, READ_BUFFER_SIZE)) > 0 ) {
125126 // Process the received data
126127 // write(STDOUT_FILENO, readBuff, nbytes);
127128 readBuff[nbytes] = 0x00 ;
@@ -130,7 +131,7 @@ std::wstring recvHttpResponse(const int &socket){
130131 }
131132 if (nbytes == -1 ) {
132133 perror (" read" );
133- close (socket );
134+ close (tcpSocket );
134135 return std::wstring ();
135136 }
136137 return s2ws (tmpDataRead);
@@ -139,14 +140,14 @@ std::wstring recvHttpResponse(const int &socket){
139140
140141// ============================ PUBLIC API ============================
141142
142- std::wstring httpPost (const std::wstring &url, const std::wstring &port, const std::wstring &request) {
143+ std::wstring HttpPost::operator () (const std::wstring &url, const std::wstring &port, const std::wstring &request) {
143144
144145 const int tcpSocket = createTcpSocket ();
145146 if (tcpSocket == -1 ){
146147 exit (-1 );
147148 }
148149
149- int retValue = connectTcp (tcpSocket, url, port);
150+ int retValue = connectTcp (url, port);
150151 if (retValue == -1 ){
151152 exit (-1 );
152153 }
@@ -155,11 +156,11 @@ std::wstring httpPost(const std::wstring &url, const std::wstring &port, const s
155156 return std::wstring ();
156157 }
157158
158- retValue = sendHttpRequest (tcpSocket, request);
159+ retValue = sendHttpRequest (request);
159160 if (retValue == -1 ){
160161 return std::wstring ();
161162 }
162- std::wstring readBuffStr = recvHttpResponse (tcpSocket );
163+ std::wstring readBuffStr = recvHttpResponse ();
163164 if (readBuffStr.empty ()){
164165 return std::wstring ();
165166 }
@@ -172,4 +173,8 @@ std::wstring httpPost(const std::wstring &url, const std::wstring &port, const s
172173 }
173174 close (tcpSocket);
174175 return decodedData;
176+ }
177+
178+ HttpPost::~HttpPost (){
179+ close (tcpSocket);
175180}
0 commit comments