kopia lustrzana https://github.com/SP8EBC/ParaTNC
http client daily
rodzic
00f91241b2
commit
f899378ff8
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* http_client_headers.h
|
||||
*
|
||||
* Created on: Mar 25, 2022
|
||||
* Author: mateusz
|
||||
*/
|
||||
|
||||
#ifndef INCLUDE_HTTP_CLIENT_HTTP_CLIENT_HEADERS_H_
|
||||
#define INCLUDE_HTTP_CLIENT_HTTP_CLIENT_HEADERS_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum http_client_method {
|
||||
HTTP_GET,
|
||||
HTTP_POST,
|
||||
HTTP_PUT,
|
||||
HTTP_DELETE
|
||||
} http_client_method_t;
|
||||
|
||||
void http_client_headers_preamble(http_client_method_t method, char * url, uint8_t url_ln, char * output, uint8_t output_ln);
|
||||
void http_client_headers_user_agent(char * output, uint8_t output_ln);
|
||||
|
||||
|
||||
#endif /* INCLUDE_HTTP_CLIENT_HTTP_CLIENT_HEADERS_H_ */
|
|
@ -1,6 +1,8 @@
|
|||
#include "http_client/http_client.h"
|
||||
#include "http_client/http_client_rx_callback.h"
|
||||
|
||||
#include "gsm/sim800c_tcpip.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
typedef enum http_client_state {
|
||||
|
@ -51,6 +53,14 @@ uint8_t http_client_ignore_content_on_http_error;
|
|||
*/
|
||||
const char * http_client_default_port = "80";
|
||||
|
||||
/**
|
||||
* Local, static buffers to fetch domain name or IP address from URI
|
||||
*/
|
||||
#define HOSTNAME_LN 32
|
||||
#define PORT_LN 5
|
||||
static char http_client_hostname[HOSTNAME_LN];
|
||||
static char http_client_port[PORT_LN];
|
||||
|
||||
/**
|
||||
* This functions splits the URL string into hostname and path
|
||||
*
|
||||
|
@ -82,8 +92,84 @@ static uint16_t http_client_split_hostname_and_path(char * input, uint16_t input
|
|||
|
||||
static void http_client_get_port_from_url(char * input, uint16_t input_ln, char * port, uint16_t port_ln) {
|
||||
|
||||
char temp[5] = {0, 0, 0, 0, 0};
|
||||
|
||||
// get split point
|
||||
uint16_t split_point = http_client_split_hostname_and_path(input, input_ln);
|
||||
|
||||
if (split_point != 0xFFFF) {
|
||||
// check last character before split point
|
||||
char last_character = *(input + split_point - 1);
|
||||
|
||||
// clear target buffer
|
||||
memset (port, 0x00, port_ln);
|
||||
|
||||
// if any port has been provided by a user
|
||||
if (last_character >= '0' && last_character <= '9' ) {
|
||||
|
||||
// copy maximum of 5 characters until ':'
|
||||
for (short i = 1; i < 6; i++) {
|
||||
|
||||
// get current character
|
||||
last_character = *(input + split_point - i);
|
||||
|
||||
// break on any non digit character
|
||||
if (last_character < '0' || last_character > '9') {
|
||||
break;
|
||||
}
|
||||
|
||||
// copy to temporary buffer
|
||||
temp[i - 1] = last_character;
|
||||
}
|
||||
|
||||
// copy port number into target buffer
|
||||
memcpy(port, temp, 5);
|
||||
}
|
||||
else {
|
||||
// copy default port
|
||||
memcpy(port, http_client_default_port, 2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void http_client_get_address_from_url(char * input, uint16_t input_ln, char * address, uint16_t address_ln) {
|
||||
|
||||
int first_index = 0, last_index = 0;
|
||||
|
||||
// get split point
|
||||
uint16_t split_point = http_client_split_hostname_and_path(input, input_ln);
|
||||
|
||||
// if split point is valid it also means that URL starts from 'http://'
|
||||
if (split_point != 0xFFFF) {
|
||||
|
||||
// first position of an URL
|
||||
first_index = 7;
|
||||
|
||||
// rewind to either ':' or split point
|
||||
for (last_index = first_index; last_index <= split_point; last_index ++ ) {
|
||||
|
||||
// fetch current character
|
||||
char current_ = *(input + last_index);
|
||||
|
||||
// check if
|
||||
if (current_ == ':' || current_ == '/') {
|
||||
|
||||
// rewind one character
|
||||
current_--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// check if output fits data
|
||||
if (last_index - first_index < address_ln) {
|
||||
// if yes copy it
|
||||
memcpy(address, input + first_index, last_index - first_index);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void http_client_init(gsm_sim800_state_t * state, srl_context_t * serial_context, uint8_t ignore_content_on_http_error) {
|
||||
|
@ -100,9 +186,15 @@ uint8_t http_client_async_get(char * url, uint8_t url_ln, uint16_t response_ln_l
|
|||
|
||||
uint8_t out = 0;
|
||||
|
||||
uint8_t connect_result = -1;
|
||||
|
||||
// simple check if url seems to be corrected or not
|
||||
if (split_point != 0xFFFF && http_client_state == HTTP_CLIENT_READY ) {
|
||||
|
||||
// clear local buffers
|
||||
memset(http_client_hostname, 0x00, HOSTNAME_LN);
|
||||
memset(http_client_port, 0x00, PORT_LN);
|
||||
|
||||
// check if module is busy on other TCP/IP connection
|
||||
if (*http_client_deticated_sim800_state == SIM800_TCP_CONNECTED && force_disconnect_on_busy != 0) {
|
||||
// if client is connected end a user wants to force disconnect
|
||||
|
@ -112,7 +204,18 @@ uint8_t http_client_async_get(char * url, uint8_t url_ln, uint16_t response_ln_l
|
|||
out = HTTP_CLIENT_RET_TCPIP_BSY;
|
||||
}
|
||||
|
||||
gsm_sim800_tcpip_connect(url, url_ln, url, http_client_http_code, http_client_deticated_serial_context, http_client_deticated_sim800_state);
|
||||
// get hotsname from URL (URI)
|
||||
http_client_get_address_from_url(url, url_ln, http_client_hostname, HOSTNAME_LN);
|
||||
http_client_get_port_from_url(url, url_ln, http_client_port, PORT_LN);
|
||||
|
||||
// establish TCP connection to HTTP server
|
||||
connect_result = gsm_sim800_tcpip_connect(http_client_hostname, HOSTNAME_LN, http_client_port, PORT_LN, http_client_deticated_serial_context, http_client_deticated_sim800_state);
|
||||
|
||||
// if connection has been established
|
||||
if (connect_result == 0) {
|
||||
// set appropriate state
|
||||
http_client_state = HTTP_CLIENT_CONNECTED_IDLE;
|
||||
}
|
||||
}
|
||||
else if (split_point == 0xFFFF) {
|
||||
out = HTTP_CLIENT_RET_WRONG_URL;
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
/*
|
||||
* http_client_headers.c
|
||||
*
|
||||
* Created on: Mar 25, 2022
|
||||
* Author: mateusz
|
||||
*/
|
||||
|
||||
|
Ładowanie…
Reference in New Issue