29 #define DISABLE_DEBUGLOG
33 #include "syncio_http_p.h"
36 #include <gwenhywfar/misc.h>
37 #include <gwenhywfar/debug.h>
38 #include <gwenhywfar/gui.h>
39 #include <gwenhywfar/text.h>
54 GWEN_SYNCIO_HTTP *xio;
80 GWEN_SYNCIO_HTTP *xio;
82 xio=(GWEN_SYNCIO_HTTP*) p;
98 GWEN_SYNCIO_HTTP *xio;
129 GWEN_SYNCIO_HTTP *xio;
158 GWEN_SYNCIO_HTTP *xio;
164 xio->readMode=GWEN_SyncIo_Http_Mode_Idle;
172 GWEN_SYNCIO_HTTP *xio;
184 if (xio->readMode==GWEN_SyncIo_Http_Mode_Idle) {
197 xio->readMode=GWEN_SyncIo_Http_Mode_Error;
203 if (!(s && strcasecmp(s,
"HTTP/0.9")==0)) {
207 xio->readMode=GWEN_SyncIo_Http_Mode_Error;
217 xio->readMode=GWEN_SyncIo_Http_Mode_Error;
223 if (!(s && strcasecmp(s,
"HTTP/0.9")==0)) {
227 xio->readMode=GWEN_SyncIo_Http_Mode_Error;
235 if (xio->readMode==GWEN_SyncIo_Http_Mode_ChunkSize) {
239 xio->readMode=GWEN_SyncIo_Http_Mode_Error;
242 if (xio->currentReadChunkSize==0) {
262 else if (xio->currentReadChunkSize==-1) {
264 xio->readMode=GWEN_SyncIo_Http_Mode_Error;
269 xio->readMode=GWEN_SyncIo_Http_Mode_Chunk;
272 if (xio->readMode==GWEN_SyncIo_Http_Mode_Chunk) {
276 xio->readMode=GWEN_SyncIo_Http_Mode_Error;
284 if (xio->readMode==GWEN_SyncIo_Http_Mode_Body) {
288 xio->readMode=GWEN_SyncIo_Http_Mode_Error;
296 if (xio->readMode==GWEN_SyncIo_Http_Mode_Error) {
307 const uint8_t *buffer,
309 GWEN_SYNCIO_HTTP *xio;
325 if (xio->writeMode==GWEN_SyncIo_Http_Mode_Idle) {
335 xio->writeMode=GWEN_SyncIo_Http_Mode_Error;
341 if (!(s && strcasecmp(s,
"HTTP/0.9")==0)) {
344 xio->writeMode=GWEN_SyncIo_Http_Mode_Error;
350 if (xio->writeMode==GWEN_SyncIo_Http_Mode_ChunkSize) {
353 xio->writeMode=GWEN_SyncIo_Http_Mode_Error;
363 xio->writeMode=GWEN_SyncIo_Http_Mode_Chunk;
366 if (xio->writeMode==GWEN_SyncIo_Http_Mode_Chunk) {
371 xio->writeMode=GWEN_SyncIo_Http_Mode_Error;
374 xio->writeMode=GWEN_SyncIo_Http_Mode_ChunkSize;
379 if (xio->writeMode==GWEN_SyncIo_Http_Mode_Body) {
380 if ((xio->currentWriteBodySize!=-1) &&
381 (size>xio->currentWriteBodySize)) {
383 xio->writeMode=GWEN_SyncIo_Http_Mode_Error;
391 xio->writeMode=GWEN_SyncIo_Http_Mode_Error;
394 if (xio->currentWriteBodySize!=-1) {
395 xio->currentWriteBodySize-=rv;
396 if (xio->currentWriteBodySize==0)
403 if (xio->writeMode==GWEN_SyncIo_Http_Mode_Error) {
414 GWEN_SYNCIO_HTTP *xio;
465 GWEN_SYNCIO_HTTP *xio;
480 "Bad format of HTTP status (%s)", buffer);
490 while(*p && isdigit((
int)*p))
496 if (1!=sscanf(s,
"%d", &code)) {
512 GWEN_SYNCIO_HTTP *xio;
528 "Bad format of HTTP request (%s)", buffer);
542 "Bad format of HTTP request (%s)", buffer);
569 GWEN_SYNCIO_HTTP *xio;
619 GWEN_SYNCIO_HTTP *xio;
655 GWEN_SYNCIO_HTTP *xio;
672 if (p[1]==32 || p[1]==9)
692 while(*p && (*p==32 || *p==9))
696 while(*p && *p!=
':' && *p>32 && *p<127)
706 while(*p && (*p==32 || *p==9))
716 xio->readMode=GWEN_SyncIo_Http_Mode_Body;
723 xio->currentReadChunkSize=-1;
724 xio->readMode=GWEN_SyncIo_Http_Mode_ChunkSize;
729 if (xio->currentReadBodySize==0) {
733 else if (xio->currentReadBodySize==-1) {
741 if (rcode<0 || rcode>=300) {
753 GWEN_SYNCIO_HTTP *xio;
833 GWEN_SYNCIO_HTTP *xio;
873 xio->currentReadChunkSize=csize;
882 GWEN_SYNCIO_HTTP *xio;
897 if (size>xio->currentReadChunkSize)
898 size=xio->currentReadChunkSize;
906 xio->currentReadChunkSize-=rv;
907 if (xio->currentReadBodySize>0)
908 xio->currentReadBodySize-=rv;
910 if (xio->currentReadChunkSize==0) {
927 xio->readMode=GWEN_SyncIo_Http_Mode_ChunkSize;
936 GWEN_SYNCIO_HTTP *xio;
953 if ((xio->currentReadBodySize>=0) &&
954 (size>xio->currentReadBodySize)) {
956 size, xio->currentReadBodySize);
957 size=xio->currentReadBodySize;
966 if (xio->currentReadBodySize>=0)
967 xio->currentReadBodySize-=rv;
969 if (xio->currentReadBodySize==0)
979 GWEN_SYNCIO_HTTP *xio;
1034 GWEN_SYNCIO_HTTP *xio;
1064 snprintf(numbuf,
sizeof(numbuf),
"%d ", i);
1088 GWEN_SYNCIO_HTTP *xio;
1107 xio->writeMode=GWEN_SyncIo_Http_Mode_Body;
1112 xio->currentWriteBodySize=i;
1137 xio->writeMode=GWEN_SyncIo_Http_Mode_ChunkSize;
1149 snprintf(numbuf,
sizeof(numbuf),
"%d", i);
1185 if (xio->currentWriteBodySize==0)
1194 GWEN_SYNCIO_HTTP *xio;
1209 snprintf(numbuf,
sizeof(numbuf)-1,
"%x\r\n", size);
1210 numbuf[
sizeof(numbuf)-1]=0;
1224 GWEN_SYNCIO_HTTP *xio;
1230 xio->writeMode=GWEN_SyncIo_Http_Mode_Idle;
1238 GWEN_SYNCIO_HTTP *xio;
1244 return xio->dbCommandIn;
1250 GWEN_SYNCIO_HTTP *xio;
1256 return xio->dbStatusIn;
1262 GWEN_SYNCIO_HTTP *xio;
1268 return xio->dbHeaderIn;
1274 GWEN_SYNCIO_HTTP *xio;
1280 return xio->dbCommandOut;
1286 GWEN_SYNCIO_HTTP *xio;
1292 return xio->dbStatusOut;
1298 GWEN_SYNCIO_HTTP *xio;
1304 return xio->dbHeaderOut;
1311 GWEN_SYNCIO_HTTP *xio;
1327 I18N(
"Network Operation"),
1328 I18N(
"Receiving data"),
1356 if (bodySize!=-1 && bytesRead<bodySize) {
1358 "EOF met prematurely (%d < %d)",
1359 bytesRead, bodySize);
1393 if (bodySize!=-1 && bytesRead>=bodySize) {
1406 "We received an error, but we still got data, "
1407 "so we ignore the error here");
1413 I18N(
"No message received"));
1421 I18N(
"No message received"));
1435 code, s?s:
"- no text -");
1437 I18N(
"HTTP-Status: %d (%s)"),
1438 code, s?s:
I18N(
"- no details -"));
1444 I18N(
"No HTTP status code received"));
1455 GWEN_SYNCIO_HTTP *xio;
1471 I18N(
"Network Operation"),
1472 I18N(
"Receiving data"),
1494 if (bodySize!=-1 && bytesRead<bodySize) {
1496 "EOF met prematurely (%d < %d)",
1497 bytesRead, bodySize);
1537 if (bodySize!=-1 && bytesRead>=bodySize) {
1550 "We received an error, but we still got data, "
1551 "so we ignore the error here");
1557 I18N(
"No message received"));
1565 I18N(
"No message received"));
1579 code, s?s:
"- no text -");
1581 I18N(
"HTTP-Status: %d (%s)"),
1582 code, s?s:
I18N(
"- no details -)"));
1588 I18N(
"No HTTP status code received"));
int GWEN_SyncIo_WriteForced(GWEN_SYNCIO *sio, const uint8_t *buffer, uint32_t size)
int GWEN_Gui_ProgressLog(uint32_t id, GWEN_LOGGER_LEVEL level, const char *text)
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
int GWENHYWFAR_CB GWEN_SyncIo_Http_Connect(GWEN_SYNCIO *sio)
#define GWEN_DB_FLAGS_OVERWRITE_VARS
int GWEN_SyncIo_Connect(GWEN_SYNCIO *sio)
int GWEN_Gui_ProgressAdvance(uint32_t id, uint32_t progress)
void GWEN_SyncIo_SubFlags(GWEN_SYNCIO *sio, uint32_t fl)
struct GWEN_DB_NODE GWEN_DB_NODE
int GWEN_Buffer_AllocRoom(GWEN_BUFFER *bf, uint32_t size)
uint32_t GWEN_Buffer_GetMaxUnsegmentedWrite(GWEN_BUFFER *bf)
int GWEN_SyncIo_Http_ParseHeader(GWEN_SYNCIO *sio, char *buf)
int GWEN_SyncIo_Http_RecvBodyToSio(GWEN_SYNCIO *sio, GWEN_SYNCIO *sout)
void GWEN_DB_Group_free(GWEN_DB_NODE *n)
#define GWEN_ERROR_INVALID
int GWENHYWFAR_CB GWEN_SyncIo_Http_Disconnect(GWEN_SYNCIO *sio)
GWEN_DB_NODE * GWEN_SyncIo_Http_GetDbHeaderOut(const GWEN_SYNCIO *sio)
void GWEN_SyncIo_Http_SetWriteIdle(GWEN_SYNCIO *sio)
uint32_t GWEN_Buffer_GetUsedBytes(const GWEN_BUFFER *bf)
#define GWEN_GUI_PROGRESS_ALLOW_EMBED
int GWEN_SyncIo_Read(GWEN_SYNCIO *sio, uint8_t *buffer, uint32_t size)
int GWEN_SyncIo_Http_RecvBody(GWEN_SYNCIO *sio, GWEN_BUFFER *buf)
int GWEN_SyncIo_Http_ReadStatus(GWEN_SYNCIO *sio)
int GWEN_Gui_ProgressSetTotal(uint32_t id, uint64_t total)
GWEN_SYNCIO_WRITE_FN GWEN_SyncIo_SetWriteFn(GWEN_SYNCIO *sio, GWEN_SYNCIO_WRITE_FN fn)
#define GWEN_FREE_OBJECT(varname)
GWEN_SYNCIO_CONNECT_FN GWEN_SyncIo_SetConnectFn(GWEN_SYNCIO *sio, GWEN_SYNCIO_CONNECT_FN fn)
#define GWEN_SYNCIO_FLAGS_TRANSPARENT
int GWEN_SyncIo_Http_ParseCommand(GWEN_SYNCIO *sio, const char *buffer)
uint32_t GWEN_SyncIo_GetFlags(const GWEN_SYNCIO *sio)
#define GWEN_GUI_PROGRESS_DELAY
int GWEN_Buffer_AdjustUsedBytes(GWEN_BUFFER *bf)
int GWEN_SyncIo_Http_ReadChunk(GWEN_SYNCIO *sio, uint8_t *buffer, uint32_t size)
void GWEN_SyncIo_SetStatus(GWEN_SYNCIO *sio, GWEN_SYNCIO_STATUS st)
uint32_t GWEN_Buffer_GetPos(const GWEN_BUFFER *bf)
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
GWEN_SYNCIO * GWEN_SyncIo_GetBaseIo(const GWEN_SYNCIO *sio)
char * GWEN_Buffer_GetPosPointer(const GWEN_BUFFER *bf)
void GWEN_Buffer_Reset(GWEN_BUFFER *bf)
GWEN_DB_NODE * GWEN_SyncIo_Http_GetDbStatusOut(const GWEN_SYNCIO *sio)
int GWEN_Buffer_IncrementPos(GWEN_BUFFER *bf, uint32_t i)
#define GWEN_ERROR_INTERRUPTED
GWEN_DB_NODE * GWEN_SyncIo_Http_GetDbHeaderIn(const GWEN_SYNCIO *sio)
int GWEN_SyncIo_Http_ReadBody(GWEN_SYNCIO *sio, uint8_t *buffer, uint32_t size)
int GWEN_SyncIo_Http_ParseStatus(GWEN_SYNCIO *sio, char *buffer)
#define GWEN_ERROR_NOT_CONNECTED
GWEN_SYNCIO * GWEN_SyncIo_Http_new(GWEN_SYNCIO *baseIo)
#define GWEN_ERROR_BAD_DATA
GWEN_DB_NODE * GWEN_DB_GetFirstVar(GWEN_DB_NODE *n)
GWEN_DB_NODE * GWEN_SyncIo_Http_GetDbStatusIn(const GWEN_SYNCIO *sio)
#define GWEN_NEW_OBJECT(typ, varname)
struct GWEN_SYNCIO GWEN_SYNCIO
#define DBG_DEBUG(dbg_logger, format, args...)
int GWEN_SyncIo_Http_ReadLine(GWEN_SYNCIO *sio, GWEN_BUFFER *tbuf)
int GWEN_Gui_ProgressLog2(uint32_t id, GWEN_LOGGER_LEVEL level, const char *fmt,...)
#define GWEN_ERROR_GENERIC
void GWEN_Text_LogString(const char *s, unsigned int l, const char *logDomain, GWEN_LOGGER_LEVEL lv)
#define GWEN_SYNCIO_FLAGS_PASSIVE
const char * GWEN_DB_GetCharValue(GWEN_DB_NODE *n, const char *path, int idx, const char *defVal)
GWEN_SYNCIO_STATUS GWEN_SyncIo_GetStatus(const GWEN_SYNCIO *sio)
const char * GWEN_DB_VariableName(GWEN_DB_NODE *n)
void GWEN_Buffer_free(GWEN_BUFFER *bf)
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
int GWENHYWFAR_CB GWEN_SyncIo_Http_Write(GWEN_SYNCIO *sio, const uint8_t *buffer, uint32_t size)
void GWEN_SyncIo_AddFlags(GWEN_SYNCIO *sio, uint32_t fl)
uint32_t GWEN_Gui_ProgressStart(uint32_t progressFlags, const char *title, const char *text, uint64_t total, uint32_t guiid)
int GWEN_Gui_ProgressEnd(uint32_t id)
#define GWEN_PATH_FLAGS_CREATE_VAR
int GWEN_SyncIo_Http_ReadHeader(GWEN_SYNCIO *sio)
GWEN_DB_NODE * GWEN_SyncIo_Http_GetDbCommandOut(const GWEN_SYNCIO *sio)
#define DBG_ERROR(dbg_logger, format, args...)
int GWEN_SyncIo_Http_WriteHeader(GWEN_SYNCIO *sio)
int GWEN_SyncIo_Disconnect(GWEN_SYNCIO *sio)
#define GWEN_GUI_PROGRESS_SHOW_PROGRESS
int GWEN_Text_ComparePattern(const char *w, const char *p, int sensecase)
int GWEN_DB_SetCharValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const char *val)
GWEN_SYNCIO * GWEN_SyncIo_new(const char *typeName, GWEN_SYNCIO *baseIo)
void GWENHYWFAR_CB GWEN_SyncIo_Http_FreeData(void *bp, void *p)
GWEN_DB_NODE * GWEN_DB_GetFirstValue(GWEN_DB_NODE *n)
GWEN_SYNCIO_DISCONNECT_FN GWEN_SyncIo_SetDisconnectFn(GWEN_SYNCIO *sio, GWEN_SYNCIO_DISCONNECT_FN fn)
#define DBG_INFO(dbg_logger, format, args...)
GWEN_DB_NODE * GWEN_DB_GetNextVar(GWEN_DB_NODE *n)
#define GWEN_SYNCIO_HTTP_TYPE
void GWEN_SyncIo_Http_SetReadIdle(GWEN_SYNCIO *sio)
int GWEN_DB_GetIntValue(GWEN_DB_NODE *n, const char *path, int idx, int defVal)
GWEN_DB_NODE * GWEN_DB_Group_new(const char *name)
int GWEN_Logger_GetLevel(const char *logDomain)
#define GWEN_INHERIT(bt, t)
GWEN_SYNCIO_READ_FN GWEN_SyncIo_SetReadFn(GWEN_SYNCIO *sio, GWEN_SYNCIO_READ_FN fn)
int GWENHYWFAR_CB GWEN_SyncIo_Http_Read(GWEN_SYNCIO *sio, uint8_t *buffer, uint32_t size)
int GWEN_DB_SetIntValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, int val)
#define GWEN_ERROR_USER_ABORTED
#define GWEN_ERROR_NO_DATA
int GWEN_DB_GetIntValueFromNode(const GWEN_DB_NODE *n)
int GWEN_SyncIo_Http_WriteStatus(GWEN_SYNCIO *sio)
int GWEN_SyncIo_Http_ReadCommand(GWEN_SYNCIO *sio)
int GWEN_DB_ClearGroup(GWEN_DB_NODE *n, const char *path)
#define GWEN_INHERIT_SETDATA(bt, t, element, data, fn)
int GWEN_SyncIo_Http_ReadChunkSize(GWEN_SYNCIO *sio)
int GWEN_SyncIo_Http_WriteCommand(GWEN_SYNCIO *sio)
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
GWEN_DB_NODE_TYPE GWEN_DB_GetValueType(GWEN_DB_NODE *n)
#define GWEN_INHERIT_GETDATA(bt, t, element)
GWEN_DB_NODE * GWEN_SyncIo_Http_GetDbCommandIn(const GWEN_SYNCIO *sio)
const char * GWEN_DB_GetCharValueFromNode(const GWEN_DB_NODE *n)
#define GWEN_GUI_PROGRESS_SHOW_ABORT
int GWEN_SyncIo_Http_WriteChunkSize(GWEN_SYNCIO *sio, uint32_t size)