Skip to content
Snippets Groups Projects
Commit 7b0607f7 authored by Aleksandr Lysikov's avatar Aleksandr Lysikov
Browse files

client side of http protocol over unix-socket server is ready

parent 89106b95
No related branches found
No related tags found
No related merge requests found
Subproject commit e79ecf549acae3020fb9f225c16315e45fb4b7c5 Subproject commit b94966fbf56e841135d346463b6a5495d0707747
Subproject commit e4ca02783c7f8afb13649041a91886a9b865d6d9 Subproject commit e6332e3d2b68daa0c15624507d2b7f7d64f4408d
...@@ -156,6 +156,19 @@ int execute_line(char *line) ...@@ -156,6 +156,19 @@ int execute_line(char *line)
return ((*(command->func))(argc, (const char **) argv)); return ((*(command->func))(argc, (const char **) argv));
} }
/**
* Clear and delete memory of structure cmd_state
*/
void free_cmd_state(cmd_state *cmd) {
if(!cmd->cmd_param)
return;
for(int i = 0; i < cmd->cmd_param_count; i++)
{
DAP_DELETE(cmd->cmd_param[i]);
}
DAP_DELETE(cmd);
}
/** /**
* Read and execute commands until EOF is reached. This assumes that * Read and execute commands until EOF is reached. This assumes that
* the input source has already been initialized. * the input source has already been initialized.
...@@ -206,10 +219,13 @@ int main(int argc, const char * argv[]) ...@@ -206,10 +219,13 @@ int main(int argc, const char * argv[])
printf("start node_cli_post_command()\n"); printf("start node_cli_post_command()\n");
cmd_state *cmd = DAP_NEW_Z(cmd_state); cmd_state *cmd = DAP_NEW_Z(cmd_state);
cmd->cmd_name = "cmd1"; cmd->cmd_name = "cmd1";
cmd->cmd_param = "t1 -t2"; cmd->cmd_param_count = 2;
cmd->cmd_param = DAP_NEW_Z_SIZE(char*, cmd->cmd_param_count * sizeof(char*));
cmd->cmd_param[0] = strdup("t2-t1");
cmd->cmd_param[1] = strdup("-opt");
int a = node_cli_post_command(cparam, cmd); int a = node_cli_post_command(cparam, cmd);
printf("node_cli_post_command()=%d\n",a); printf("node_cli_post_command()=%d\n", a);
DAP_DELETE(cmd); free_cmd_state(cmd);
} }
COMMAND *command = NULL; COMMAND *command = NULL;
...@@ -231,3 +247,4 @@ int main(int argc, const char * argv[]) ...@@ -231,3 +247,4 @@ int main(int argc, const char * argv[])
node_cli_desconnect(cparam); node_cli_desconnect(cparam);
return 0; return 0;
} }
...@@ -26,10 +26,16 @@ ...@@ -26,10 +26,16 @@
#include <stdint.h> #include <stdint.h>
// command description // command description
typedef struct cmd_state_{ typedef struct cmd_state_ {
char *cmd_name; char *cmd_name;
char *cmd_param; char **cmd_param;
int cmd_param_count;
int ret_code; int ret_code;
uint8_t *ret_str; uint8_t *ret_str;
size_t ret_str_len; size_t ret_str_len;
} cmd_state; } cmd_state;
/**
* Clear and delete memory of structure cmd_state
*/
void free_cmd_state(cmd_state *cmd);
...@@ -89,17 +89,16 @@ connect_param* node_cli_connect(void) ...@@ -89,17 +89,16 @@ connect_param* node_cli_connect(void)
curl_global_init(CURL_GLOBAL_DEFAULT); curl_global_init(CURL_GLOBAL_DEFAULT);
connect_param *param = DAP_NEW_Z(connect_param); connect_param *param = DAP_NEW_Z(connect_param);
CURL *curl_handle = curl_easy_init(); CURL *curl_handle = curl_easy_init();
int ret = curl_easy_setopt(curl_handle, CURLOPT_UNIX_SOCKET_PATH, UNIX_SOCKET_FILE); int ret = curl_easy_setopt(curl_handle, CURLOPT_UNIX_SOCKET_PATH, UNIX_SOCKET_FILE); // unix socket mode
//ret = curl_easy_setopt(curl_handle, CURLOPT_BUFFERSIZE, 256); curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 20L); // complete within 20 seconds
//ret = curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 1000000); ret = curl_easy_setopt(curl_handle, CURLOPT_CONNECT_ONLY, 1L); // connection only
// curl_easy_setopt(curl_handle, CURLOPT_URL, "http://localhost:8079/");
ret = curl_easy_setopt(curl_handle, CURLOPT_CONNECT_ONLY, 1L);
ret = curl_easy_setopt(curl_handle, CURLOPT_URL, "http:/localhost/connect"); ret = curl_easy_setopt(curl_handle, CURLOPT_URL, "http:/localhost/connect");
// execute request // execute request
ret = curl_easy_perform(curl_handle); ret = curl_easy_perform(curl_handle);
if(!ret) if(!ret)
{ {
param->curl = curl_handle; param->curl = curl_handle;
curl_easy_setopt(curl_handle, CURLOPT_CONNECT_ONLY, 0L); // disable mode - connection only
} }
else else
{ {
...@@ -126,7 +125,7 @@ int node_cli_post_command(connect_param *conn, cmd_state *cmd) ...@@ -126,7 +125,7 @@ int node_cli_post_command(connect_param *conn, cmd_state *cmd)
CURL *curl = conn->curl; CURL *curl = conn->curl;
ret = curl_easy_setopt(curl, CURLOPT_HEADER, 0L); // don't get header in the body ret = curl_easy_setopt(curl, CURLOPT_HEADER, 0L); // don't get header in the body
ret = curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip, deflate"); // allow receive of compressed data //ret = curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip, deflate"); // allow receive of compressed data
//callback functions to receive data //callback functions to receive data
ret = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteHttpMemoryCallback); // callback for the data read ret = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteHttpMemoryCallback); // callback for the data read
//callback functions to receive header //callback functions to receive header
...@@ -136,28 +135,33 @@ int node_cli_post_command(connect_param *conn, cmd_state *cmd) ...@@ -136,28 +135,33 @@ int node_cli_post_command(connect_param *conn, cmd_state *cmd)
ret = curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void * )cmd); ret = curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void * )cmd);
ret = curl_easy_setopt(curl, CURLOPT_USERAGENT, "kelvin-console 1.0"); ret = curl_easy_setopt(curl, CURLOPT_USERAGENT, "kelvin-console 1.0");
ret = curl_easy_setopt(curl, CURLOPT_POST, 1); // POST request - optional if CURLOPT_POSTFIELDS will be // ret = curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);// GET запрос
// ret = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);// удалённый сервер не будет проверять наш сертификат. В противном случае необходимо этот самый сертификат послать.
//ret = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
char *post_data = NULL; char *post_data = NULL;
ret = curl_easy_setopt(curl, CURLOPT_POST, 1); // POST request - optional if CURLOPT_POSTFIELDS will be
size_t post_data_len = 0; size_t post_data_len = 0;
add_mem_data((uint8_t**)&post_data, &post_data_len, cmd->cmd_name, strlen(cmd->cmd_name)); add_mem_data((uint8_t**) &post_data, &post_data_len, cmd->cmd_name, strlen(cmd->cmd_name));
if(cmd->cmd_param) if(cmd->cmd_param) {
{ for(int i = 0; i < cmd->cmd_param_count; i++) {
add_mem_data((uint8_t**)&post_data, &post_data_len, "\r\n", 2); if(cmd->cmd_param[i]) {
add_mem_data((uint8_t**)&post_data, &post_data_len, cmd->cmd_param, strlen(cmd->cmd_param)); add_mem_data((uint8_t**) &post_data, &post_data_len, "\r\n", 2);
add_mem_data((uint8_t**) &post_data, &post_data_len, cmd->cmd_param[i], strlen(cmd->cmd_param[i]));
}
}
add_mem_data((uint8_t**) &post_data, &post_data_len, "\r\n\r\n", 4);
if(post_data)
ret = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); // data for POST request
if(post_data_len >= 0)
ret = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long )post_data_len); // if need a lot to send: CURLOPT_POSTFIELDSIZE_LARGE
// sending request and receiving the http page (filling cmd)
ret = curl_easy_perform(curl); // curl_easy_send
free(post_data);
return ret;
} }
add_mem_data((uint8_t**)&post_data, &post_data_len, "\r\n", 2); return -1;
if(post_data)
ret = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); // data for POST request
if(post_data_len >= 0)
ret = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long )post_data_len); // if need a lot to send: CURLOPT_POSTFIELDSIZE_LARGE
// sending request and receiving the http page (filling cmd)
ret = curl_easy_perform(curl); // curl_easy_send
free(post_data);
// net_func_exit_if_err(ret);
// // распарсить принятый http-заголовок
// page->header = http_parse_header(page);
return ret;
} }
int node_cli_desconnect(connect_param *param) int node_cli_desconnect(connect_param *param)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment