How to write a C Program to Extract query from request-target in C Programming Language ?
Solution:
- // TODO: extract query from request-target
- char* beg_query = strchr(reqTarget, '=');
- ssize_t start_q = (ssize_t)(beg_query - reqTarget);
- char* end_query = strchr(reqTarget, '\0');
- ssize_t last_q = (ssize_t)(end_query - reqTarget);
- ssize_t length_q = last_q - start_q;
- char query[length_q];
- ssize_t n = 0;
- if (beg_query == NULL)
- {
- query[0] = '\0';
- }
- else
- {
- for(ssize_t i = start_q + 1; i < length_q; i++)
- {
- query[n] = i;
- n++;
- }
- }
- query[length_q + 1] = '\0';
- printf("%s\n", query);
- // TODO: concatenate root and absolute-path
- ssize_t length_root = strlen(argv[optind]);
- char* start_resour = strchr(reqTarget, '/');
- ssize_t start_resourLoc = (ssize_t)(start_resour - reqTarget);
- char* end_resour = strchr(reqTarget,'\0');
- ssize_t end_resourLoc = (ssize_t)(end_resour - reqTarget);
- char* end_resour2 = strchr(reqTarget, '?');
- ssize_t end_resourLoc2 = (ssize_t)(end_resour2 - reqTarget);
- ssize_t length_resour = 0;
- ssize_t j;
- char new_resour[length_root + length_resour];
- if (end_resour2 != NULL)
- {
- length_resour = end_resourLoc2 - start_resourLoc;
- }
- else if (end_resour2 == NULL)
- {
- length_resour = end_resourLoc - start_resourLoc;
- }
- for(ssize_t i = 0; i < length_resour; i++)
- {
- new_resour[i] = reqTarget[i];
- }
- char path[length_root + length_resour];
- for(j = 0; j < length_root; j++)
- {
- path[j] = argv[optind][j];
- }
- for(; j < length_root + length_resour; j++)
- {
- path[j] = new_resour[j - length_root];
- }