I have this function that takes a full url and splits it into a host and an endpoint to be used in a https request
void parser(char * result, char * host, char * endpoint)
{
int i=0,j=0;
int size = strlen(result);
if (strstr(result , "https://"))
{
for (i=8; i < size; i++)
{
if (result[i] == '/') break;
host[j] = result[i];
j++;
}
host[j] = '\0';
j=0;
while ( i < strlen(result))
{
endpoint[j] = result[i];
i++,j++;
}
endpoint[j] = '\0';
}
}
the host and endpoint are copied to the relevant buffer correctly but when using the endpoint it results in a 400 status code (bad request)
screenshot as you can see in the picture the request is malformed
this is how the request string is created
char * request = (char *)malloc(8192);
sprintf(request , "%s %s HTTP/1.1\n%s\r\n\r\n%s", "GET", endpoint, "","");
as you can see in the picture the HTTP/1.1 is inserted in the endpoint after filename%3 which causes the request to be invalid and return 400 status code
Any idea why is this happening and how to fix this problem?
https://objects.githubusercontent.com
/github-production-release-asset-2e65be/21611723/f7cfd480-a7ea-11ea-852c-52df94ee4644?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220728%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220728T104250Z&X-Amz-Expires=300&X-Amz-Signature=7dbec7d58625f02f8b4dba25f74a9b3cc688184704c228f256a6a855ab41f19c&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=21611723&response-content-disposition=attachment%3B%20filename%3DQuasar.v1.4.0.zip&response-content-type=application%2Foctet-stream
URL parsed https://objects.githubusercontent.com ===> objects.githubusercontent.com:80
GET /github-production-release-asset-2e65be/21611723/f7cfd480-a7ea-11ea-852c-52df94ee4644?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220728%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220728T104250Z&X-Amz-Expires=300&X-Amz-Signature=7dbec7d58625f02f8b4dba25f74a9b3cc688184704c228f256a6a855ab41f19c&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=21611723&response-content-disposition=attachment%3B%20filename%3DQuasa HTTP/1.1 ip&response-content-type=application%2Foctet-stream
Host: objects.githubusercontent.com
Connection: close
HTTP/1.1 400 Bad Request
Connection: close
Content-Length: 11
content-type: text/plain; charset=utf-8
Bad Request
HTTP/1.1\nshould beHTTP/1.1\r\nin order to comply with HTTP. - Andreas Wenzelparser(probably endpoint is allocated too small and someone overwrites it at the end), so please show that code. Best is a minimal reproducible example. Side notes: a)strstralso finds "https://" in the middle ofresult, b)resultis a strange name for the input parameter, c)strlen(...)in every loop run results in bad performance. - Werner Henze