Skip to content

Commit

Permalink
Return enum type; mark no-content-length + close connections to colle…
Browse files Browse the repository at this point in the history
…ct data better
  • Loading branch information
d3x0r committed Jun 11, 2024
1 parent 0e9ddde commit 05f7a05
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion include/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ LOGICAL HTTPAPI AddHttpData( HTTPState pHttpState, CPOINTER buffer, size_t size
to 'content\-length' meta tag.
FALSE : Still collecting full packet */
//HTTP_EXPORT int HTTPAPI ProcessHttp( HTTPState pHttpState );
HTTP_EXPORT int HTTPAPI ProcessHttp( HTTPState pHttpState, int (*send)(uintptr_t psv, CPOINTER buf, size_t len), uintptr_t psv );
HTTP_EXPORT enum ProcessHttpResult HTTPAPI ProcessHttp( HTTPState pHttpState, int (*send)(uintptr_t psv, CPOINTER buf, size_t len), uintptr_t psv );

HTTP_EXPORT /* Gets the specific result code at the header of the packet -
http 2.0 OK sort of thing. */
Expand Down
37 changes: 23 additions & 14 deletions src/typelib/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ void GatherHttpData( struct HttpState *pHttpState )
pMergedLine = SegConcat( NULL, pNewLine, 0, GetTextSize( pHttpState->partial ) + GetTextSize( pInput ) );
LineRelease( pNewLine );
pHttpState->partial = pMergedLine;
pHttpState->content = pHttpState->partial;
pHttpState->content_length = GetTextSize( pHttpState->content );
if( !pHttpState->flags.no_content_length ) {
pHttpState->content = pHttpState->partial;
pHttpState->content_length = GetTextSize( pHttpState->content );
}
}
unlockHttp( pHttpState );
}
Expand Down Expand Up @@ -236,11 +238,12 @@ void ProcessURL_CGI( struct HttpState *pHttpState, PLIST *cgi_fields,PTEXT *ppar
}

//int ProcessHttp( struct HttpState *pHttpState )
int ProcessHttp( struct HttpState *pHttpState, int ( *send )( uintptr_t psv, CPOINTER buf, size_t len ), uintptr_t psv )
enum ProcessHttpResult ProcessHttp( struct HttpState *pHttpState, int ( *send )( uintptr_t psv, CPOINTER buf, size_t len ), uintptr_t psv )
{
lockHttp( pHttpState );
if( pHttpState->final )
{
//lprintf( "Process HTTP in final..." );
//if( !pHttpState->method )
{
//lprintf( "Reading more, after returning a packet before... %d", pHttpState->response_version );
Expand All @@ -251,7 +254,7 @@ int ProcessHttp( struct HttpState *pHttpState, int ( *send )( uintptr_t psv, CPO
if( pHttpState->flags.success && !pHttpState->returned_status ) {
unlockHttp( pHttpState );
pHttpState->returned_status = 1;
return pHttpState->numeric_code;
return (enum ProcessHttpResult)pHttpState->numeric_code;
}
}
else {
Expand All @@ -269,7 +272,7 @@ int ProcessHttp( struct HttpState *pHttpState, int ( *send )( uintptr_t psv, CPO
}
}
unlockHttp( pHttpState );

//lprintf( "return http nothing %d %d %d", pHttpState->content_length, pHttpState->flags.success, pHttpState->returned_status );
return HTTP_STATE_RESULT_NOTHING;
}
else
Expand Down Expand Up @@ -371,7 +374,7 @@ int ProcessHttp( struct HttpState *pHttpState, int ( *send )( uintptr_t psv, CPO
}
else
{
//lprintf( "Parsing http state content for something.." );
//lprintf( "Parsing http state request for something.." );
pLine = SegCreate( pos - start - pHttpState->bLine );
MemCpy( line = GetText( pLine ), c + start, (pos - start - pHttpState->bLine)*sizeof(TEXTCHAR));
line[pos-start- pHttpState->bLine] = 0;
Expand Down Expand Up @@ -623,7 +626,7 @@ int ProcessHttp( struct HttpState *pHttpState, int ( *send )( uintptr_t psv, CPO
) )
{
pHttpState->returned_status = 1;
lprintf( "return http %d",pHttpState->numeric_code );
//lprintf( "return http %d l:%d nl:%d",pHttpState->numeric_code, pHttpState->content_length, pHttpState->flags.no_content_length );
if( pHttpState->numeric_code == 500 )
return HTTP_STATE_INTERNAL_SERVER_ERROR;
if( pHttpState->content && (pHttpState->numeric_code == 200) ) {
Expand All @@ -635,7 +638,7 @@ int ProcessHttp( struct HttpState *pHttpState, int ( *send )( uintptr_t psv, CPO
return HTTP_STATE_RESOURCE_NOT_FOUND;
if( pHttpState->numeric_code == 400 )
return HTTP_STATE_BAD_REQUEST;
return pHttpState->numeric_code;
return (enum ProcessHttpResult)pHttpState->numeric_code;
}
//lprintf( "return http nothing" );
return HTTP_STATE_RESULT_NOTHING;
Expand All @@ -644,6 +647,7 @@ int ProcessHttp( struct HttpState *pHttpState, int ( *send )( uintptr_t psv, CPO
LOGICAL AddHttpData( struct HttpState *pHttpState, CPOINTER buffer, size_t size )
{
lockHttp( pHttpState );
//lprintf( "AddHttpData:%d", size );
pHttpState->last_read_tick = timeGetTime();
if( pHttpState->read_chunks )
{
Expand Down Expand Up @@ -773,7 +777,7 @@ LOGICAL AddHttpData( struct HttpState *pHttpState, CPOINTER buffer, size_t size
unlockHttp( pHttpState );
if( pHttpState->final ) {
// this will cause it to wait until 'endhttp' to process next block.
//lprintf( "still handling a previous requet in add data", pHttpState->pc[0] );
//lprintf( "still handling a previous request in add data", pHttpState->pc[0] );
return FALSE;
}
return TRUE;
Expand Down Expand Up @@ -801,7 +805,7 @@ void EndHttp( struct HttpState *pHttpState )
pHttpState->bLine = 0;
pHttpState->final = 0;
pHttpState->response_version = 0;
pHttpState->flags.no_content_length = 0;
pHttpState->flags.no_content_length = 1;
pHttpState->content_length = 0;
LineRelease( pHttpState->method );
pHttpState->method = NULL;
Expand Down Expand Up @@ -1069,17 +1073,18 @@ static void CPROC HttpReader( PCLIENT pc, POINTER buffer, size_t size )
LogBinary( (const uint8_t*) buffer, size );
}
#endif
//lprintf( "adding data:%d", size );
if( AddHttpData( state, buffer, size ) )
if( ProcessHttp( state, NULL, 0 ) ) // this shouldn't cause any auto send?
if( AddHttpData( state, buffer, size ) ) {
enum ProcessHttpResult r;
if( r = ProcessHttp( state, NULL, 0 ) ) // this shouldn't cause any auto send?
{
//lprintf( "this is where we should close and not end... %d %d",state->flags.close , !state->flags.keep_alive );
//lprintf( "this is where we should close and not end...%d %d %d",r, state->flags.close , !state->flags.keep_alive );
if( state->flags.close || !state->flags.keep_alive) {
RemoveClient( state->pc[0]);
return;
} else
EndHttp( state );
}
}
}

// read is handled by the SSL layer instead of here. Just trust that someone will give us data later
Expand All @@ -1094,7 +1099,9 @@ static void CPROC HttpReaderClose( PCLIENT pc )
struct HttpState *data = (struct HttpState *)GetNetworkLong( pc, 0 );
if( !data ) return;
PCLIENT *ppc = data->pc;// (PCLIENT*)GetNetworkLong( pc, 0 );
//lprintf( "HTTP Close Event" );
if( data->flags.no_content_length ) { // data is collected into 'partial' until close
//lprintf( "HTTP Close event... collecting data...");
GatherHttpData( data );
data->content_length = GetTextSize( data->partial );
//lprintf( "at close what is content length? %d", data->content_length );
Expand Down Expand Up @@ -1427,10 +1434,12 @@ HTTPState GetHttpsQueryEx( PTEXT address, PTEXT url, const char* certChain, stru
// wait for response.
while( state->request_socket && !state->closed
&& ( state->last_read_tick > ( timeGetTime() - options->timeout ) ) ) {
//lprintf( "waiting for response 1000 second %d", options->timeout );
WakeableSleep( 1000 );
}
//lprintf( "Request has completed.... %p %p %d", pc, state->content, state->closed );
if( state->request_socket && !state->closed ) {
//lprintf( "Closing in got response?" );
RemoveClient( state->request_socket ); // this shouldn't happen... it should have ben closed already.
}

Expand Down

0 comments on commit 05f7a05

Please sign in to comment.