c/quickwebserver.c
/**
 * Parsing HTTP request and creating JS object with
 * request data (url, method, headers)
 * 
 */
Parsing HTTP request and creating JS object with
request data (url, method, headers)
Parsing HTTP request and creating JS object with request data ( url method headers )
JSValue parseHttp(struct http_request_s* request) {
	JSValue returnObject;
	returnObject = JS_NewObject(QWS.serverContext);
	http_string_t target = http_request_target(request);
	char url[target.len];
	stringSlice(url, target.buf, target.len);
	JS_DefinePropertyValueStr(QWS.serverContext, returnObject, "url",
                                  JS_NewString(QWS.serverContext, url),
                                  JS_PROP_C_W_E);
	http_string_t method = http_request_method(request);
	char request_method[method.len];
	stringSlice(request_method, method.buf, method.len);
	JS_DefinePropertyValueStr(QWS.serverContext, returnObject, "method",
                                  JS_NewString(QWS.serverContext, request_method),
                                  JS_PROP_C_W_E);
	http_string_t body = http_request_body(request);
	JS_DefinePropertyValueStr(QWS.serverContext, returnObject, "body",
                                  JS_NewString(QWS.serverContext, body.buf),
                                  JS_PROP_C_W_E);
	JSValue headersObject;
	headersObject = JS_NewObject(QWS.serverContext);
	http_string_t key, val;
	int iter = 0;
	while (http_request_iterate_headers(request, &key, &val, &iter)) {
	    char header_key[key.len + 1], header_value[val.len + 1];
	    char *header_key_p = header_key;
	    char *header_value_p = header_value;
	    stringSlice(header_key_p, key.buf, key.len);
	    stringSlice(header_value_p, val.buf, val.len);
		JS_DefinePropertyValueStr(QWS.serverContext, headersObject, header_key,
								  JS_NewString(QWS.serverContext, header_value),
								  JS_PROP_C_W_E);
	}
	JS_DefinePropertyValueStr(QWS.serverContext, returnObject, "headers", headersObject, JS_PROP_C_W_E);
	return returnObject;
}
JSValue parseHttp ( struct http_request_s * request ) { JSValue returnObject ; returnObject = JS_NewObject ( QWS . serverContext ) ; http_string_t target = http_request_target ( request ) ; char url [ target . len ] ; stringSlice ( url , target . buf , target . len ) ; JS_DefinePropertyValueStr ( QWS . serverContext , returnObject , " " , JS_NewString ( QWS . serverContext , url ) , JS_PROP_C_W_E ) ; http_string_t method = http_request_method ( request ) ; char request_method [ method . len ] ; stringSlice ( request_method , method . buf , method . len ) ; JS_DefinePropertyValueStr ( QWS . serverContext , returnObject , " " , JS_NewString ( QWS . serverContext , request_method ) , JS_PROP_C_W_E ) ; http_string_t body = http_request_body ( request ) ; JS_DefinePropertyValueStr ( QWS . serverContext , returnObject , " " , JS_NewString ( QWS . serverContext , body . buf ) , JS_PROP_C_W_E ) ; JSValue headersObject ; headersObject = JS_NewObject ( QWS . serverContext ) ; http_string_t key , val ; int iter = 0 ; while ( http_request_iterate_headers ( request , & key , & val , & iter ) ) { char header_key [ key . len + 1 ] , header_value [ val . len + 1 ] ; char * header_key_p = header_key ; char * header_value_p = header_value ; stringSlice ( header_key_p , key . buf , key . len ) ; stringSlice ( header_value_p , val . buf , val . len ) ; JS_DefinePropertyValueStr ( QWS . serverContext , headersObject , header_key , JS_NewString ( QWS . serverContext , header_value ) , JS_PROP_C_W_E ) ; } JS_DefinePropertyValueStr ( QWS . serverContext , returnObject , " " , headersObject , JS_PROP_C_W_E ) ; return returnObject ; }
Parsing HTTP request and creating JS object with
 request data (url, method, headers)
Parsing HTTP request and creating JS object with request data ( url method headers )
// Getting URL  // Getting request method // Getting request body // Getting all the headers