QByteArray HttpSessionStore::getSessionId(HttpRequest& request, HttpResponse& response) {
    // The session ID in the response has priority because this one will be used in the next request.
    mutex.lock();
    // Get the session ID from the response cookie
    QByteArray sessionId=response.getCookies().value(cookieName).getValue();
    if (sessionId.isEmpty()) {
        // Get the session ID from the request cookie
        sessionId=request.getCookie(cookieName);
    }
    // Clear the session ID if there is no such session in the storage.
    if (!sessionId.isEmpty()) {
        if (!sessions.contains(sessionId)) {
            qDebug("HttpSessionStore: received invalid session cookie with ID %s",sessionId.data());
            sessionId.clear();
        }
    }
    mutex.unlock();
    return sessionId;
}
Example #2
0
static int mod_ffeadcpp_method_handler (request_rec *r)
{
	string port = CastUtil::lexical_cast<string>(r->server->port);

	//signal(SIGSEGV,signalSIGSEGV);
	string content;

	apr_bucket_brigade *bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
	for ( ; ; ) {
		apr_bucket* b ;
		bool done = false;
		ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES, APR_BLOCK_READ, HUGE_STRING_LEN);
		for ( b = APR_BRIGADE_FIRST(bb);b != APR_BRIGADE_SENTINEL(bb);b = APR_BUCKET_NEXT(b) )
		{
			size_t bytes ;
			const char* buf = "\0";
			if ( APR_BUCKET_IS_EOS(b) )
			{
				done = true;
			}
			else if (apr_bucket_read(b, &buf, &bytes, APR_BLOCK_READ)== APR_SUCCESS )
			{
				content += string(buf, 0, bytes);
			}
		}

		if (done)
		  break;
		else
		  apr_brigade_cleanup(bb) ;
	}
	apr_brigade_destroy(bb) ;

	HttpRequest* hreq= new HttpRequest();
	string cntpath = serverRootDirectory + "web/";
	string webpath = "URL" + cntpath;
	MyReq* req = new MyReq();
	req->r = r;
	req->htreq = hreq;

	apr_table_do(iterate_func, req, r->headers_in, NULL);

	ip_address = hreq->getHeader(HttpRequest::Host);
	string tipaddr = ip_address;
	if(port!="80")
		tipaddr += (":" + port);
	ConfigurationData::getInstance()->ip_address = tipaddr;

	string contret;
	if(content!="")
	{
		contret = hreq->buildRequest("Content",content.c_str());
		fprintf(stderr,contret.c_str());
		fflush(stderr);
	}
	hreq->buildRequest(webpath.c_str(),r->uri);
	hreq->buildRequest("Method",r->method);
	if(r->args != NULL && r->args[0] != '\0')
	{
		hreq->buildRequest("GetArguments",r->args);
	}
	hreq->buildRequest("HttpVersion", r->protocol);

	HttpResponse respo = service(hreq);
	string h1 = respo.generateResponse(hreq->getMethod(), hreq, false);

	for (int var = 0; var < (int)respo.getCookies().size(); var++)
	{
		apr_table_add(r->headers_out, "Set-cookie", respo.getCookies().at(var).c_str());
	}

	r->status_line = respo.getStatusLine().c_str();
	if(respo.getHeader(HttpResponse::ContentType)!="")
	{
		r->content_type = respo.getHeader(HttpResponse::ContentType).c_str();
	}
	r->clength = h1.length();

	fprintf(stderr,"\n\n\n\n--------------------------------------------\n");
	fprintf(stderr,hreq->getUrl().c_str());
	fprintf(stderr,"\n");
	string star =  hreq->toString();
	fprintf(stderr,star.c_str());
	fprintf(stderr,"\n--------------------------------------------\n");
	fprintf(stderr,contret.c_str());
	fflush(stderr);
	fprintf(stderr,"\n\n");
	fprintf(stderr,content.c_str());
	fprintf(stderr,"\n");
	fprintf(stderr,"ip_address = %s", tipaddr.c_str());
	fprintf(stderr,"\n");
	fprintf(stderr,"\n--------------------------------------------\n\n\n\n");
	fflush(stderr);
	fprintf(stderr, h1.c_str());
	fflush(stderr);
	delete hreq;
	delete req;

	if(h1!="")
	{
		ap_rwrite (h1.c_str(), h1.length(), r);
		return OK;
	}
	return OK;
}
static ngx_int_t ngx_http_ffeadcpp_module_handler_post_read(ngx_http_request_t *r)
{
string cntpath = "";
	cntpath.append(ffeadcpp_path.data, ffeadcpp_path.len);
	cntpath += "/web/";
	HttpRequest* req = new HttpRequest(cntpath);

	//cerr << "FFEAD in ngx_http_ffeadcpp_module_handler " << cntpath << r->uri.data << endl;
    ngx_int_t    rc;
    ngx_buf_t   *b;
    ngx_chain_t  out;

	ngx_list_part_t            *part;
    ngx_table_elt_t            *h;
    ngx_uint_t                  i;

    /*
    Get the first part of the list. There is usual only one part.
    */
    part = &r->headers_in.headers.part;
    h = part->elts;

    /*
    Headers list array may consist of more than one part,
    so loop through all of it
    */
    for (i = 0; /* void */ ; i++) {
        if (i >= part->nelts) {
            if (part->next == NULL) {
                /* The last part, search is done. */
                break;
            }

            part = part->next;
            h = part->elts;
            i = 0;
        }
//	cerr << "header -> " << string(h[i].key.data, h[i].key.len) << " = " << string(h[i].value.data, h[i].value.len) << endl;
	req->buildRequest(string(h[i].key.data, h[i].key.len), string(h[i].value.data, h[i].value.len));
    }

    string content;
	ngx_http_read_input_data(r, content);
	//cerr << "Input Request Data\n " << content << "\n======================\n" << endl;
	//cerr << "URL -> " << string(r->uri.data,r->uri.len) << endl;
	//cerr << "Method -> " << string(r->main->method_name.data, r->main->method_name.len) << endl;
	if(content!="")
	{
		req->buildRequest("Content", content.c_str());
	}
	req->buildRequest("URL",  string(r->uri.data,r->uri.len));
	req->buildRequest("Method", string(r->main->method_name.data, r->main->method_name.len));
	if(r->args.len > 0)
	{
		req->buildRequest("GetArguments", string(r->args.data,r->args.len));
	}
	req->buildRequest("HttpVersion", CastUtil::lexical_cast<string>(r->http_version));

	HttpResponse* respo = new HttpResponse;
	ServiceTask* task = new ServiceTask;
	task->handle(req, respo);
	delete task;
cerr << req->toString() << endl;
	if(respo->isDone()) {
		for (int var = 0; var < (int)respo->getCookies().size(); var++)
		{
			set_custom_header_in_headers_out(r, "Set-Cookie", respo->getCookies().at(var));
		}

		/* allocate a buffer for your response body */
		b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
		if (b == NULL) {
			return NGX_HTTP_INTERNAL_SERVER_ERROR;
		}

		/* attach this buffer to the buffer chain */
		out.buf = b;
		out.next = NULL;

		string data = respo->generateResponse(false);
		//cerr << "OUT -> " << data << endl;
		map<string,string>::const_iterator it;
		for(it=respo->getHeaders().begin();it!=respo->getHeaders().end();it++) {
			if(StringUtil::toLowerCopy(it->first)==StringUtil::toLowerCopy(HttpResponse::ContentLength)) {
				r->headers_out.content_length_n = CastUtil::lexical_cast<int>(it->second);
			} else {
				set_custom_header_in_headers_out(r, it->first, it->second);
			}
		}
		//cerr << "done writing headers" << endl;		
/* adjust the pointers of the buffer */
		b->pos = data.c_str();
		b->last = data.length();
		b->memory = 1;    /* this buffer is in memory */
		b->last_buf = 1;  /* this is the last buffer in the buffer chain */

		/* set the status line */
		r->headers_out.status = CastUtil::lexical_cast<int>(respo->getStatusCode());

		delete respo;
		delete req;
		/* send the headers of your response */
		rc = ngx_http_send_header(r);
//cerr << "done sending headers " << rc << " " << NGX_OK  <<" "<<  r->header_only  <<" " << NGX_ERROR << endl;
		if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
	        return rc;
	    }

	    /* send the buffer chain of your response */
	    return ngx_http_output_filter(r, &out);
	} else {
		u_char                    *last, *location;
		size_t                     len;
		ngx_str_t                  path;
		ngx_uint_t                 level;
		ngx_log_t                 *log;
		ngx_open_file_info_t       of;
		ngx_http_core_loc_conf_t  *clcf;

		clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

		log = r->connection->log;

		ngx_memzero(&of, sizeof(ngx_open_file_info_t));
		of.read_ahead = clcf->read_ahead;
		of.directio = clcf->directio;
		of.valid = clcf->open_file_cache_valid;
		of.min_uses = clcf->open_file_cache_min_uses;
		of.errors = clcf->open_file_cache_errors;
		of.events = clcf->open_file_cache_events;

		path.data = req->getUrl().c_str();
		path.len = ngx_strlen(path.data);
		
		delete respo;
		delete req;
		rc = NGX_OK;
		if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
			return NGX_HTTP_INTERNAL_SERVER_ERROR;
		}
cerr << string(path.data,path.len) << endl;		
		if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
		        != NGX_OK)
		{
			switch (of.err) {

			case 0:
				rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
				break;
			case NGX_ENOENT:
			case NGX_ENOTDIR:
			case NGX_ENAMETOOLONG:

				level = NGX_LOG_ERR;
				rc = NGX_HTTP_NOT_FOUND;
				break;

			case NGX_EACCES:
	#if (NGX_HAVE_OPENAT)
			case NGX_EMLINK:
			case NGX_ELOOP:
	#endif

				level = NGX_LOG_ERR;
				rc = NGX_HTTP_FORBIDDEN;
				break;

			default:

				level = NGX_LOG_CRIT;
				rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
				break;
			}

			if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
				ngx_log_error(level, log, of.err,
							  "%s \"%s\" faileddddddddddddddd", of.failed, path.data);
			}

			//return rc;
		}
		r->root_tested = !r->error_page;
cerr << "found file" << endl;
		ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd);

		/*if (of.is_dir) {

			ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");

			ngx_http_clear_location(r);

			r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
			if (r->headers_out.location == NULL) {
				return NGX_HTTP_INTERNAL_SERVER_ERROR;
			}

			len = r->uri.len + 1;

			if (!clcf->alias && clcf->root_lengths == NULL && r->args.len == 0) {
				location = path.data + clcf->root.len;

				*last = '/';

			} else {
				if (r->args.len) {
					len += r->args.len + 1;
				}

				location = ngx_pnalloc(r->pool, len);
				if (location == NULL) {
					return NGX_HTTP_INTERNAL_SERVER_ERROR;
				}

				last = ngx_copy(location, r->uri.data, r->uri.len);

				*last = '/';

				if (r->args.len) {
					*++last = '?';
					ngx_memcpy(++last, r->args.data, r->args.len);
				}
			}
			*/
			/*
			 * we do not need to set the r->headers_out.location->hash and
			 * r->headers_out.location->key fields
			 */

			/*r->headers_out.location->value.len = len;
			r->headers_out.location->value.data = location;

			return NGX_HTTP_MOVED_PERMANENTLY;
		}*/

	#if !(NGX_WIN32) /* the not regular files are probably Unix specific */

		if (!of.is_file) {
			ngx_log_error(NGX_LOG_CRIT, log, 0,
						  "\"%s\" is not a regular file", path.data);

			rc = NGX_HTTP_NOT_FOUND;
		}

	#endif

	/*	if (r->method == NGX_HTTP_POST) {
			return NGX_HTTP_NOT_ALLOWED;
		}*/

		if (rc != NGX_OK) {
			r->headers_out.status = rc;
			ngx_http_send_header(r);
			return ngx_http_send_special (r, NGX_HTTP_LAST);
		}		

		/*rc = ngx_http_discard_request_body(r);

		if (rc != NGX_OK) {
			return rc;
		}*/

		log->action = "sending response to client";

		r->headers_out.status = NGX_HTTP_OK;
		r->headers_out.content_length_n = of.size;
		r->headers_out.last_modified_time = of.mtime;

		/*if (ngx_http_set_etag(r) != NGX_OK) {
			return NGX_HTTP_INTERNAL_SERVER_ERROR;
		}*/

		if (ngx_http_set_content_type(r) != NGX_OK) {
			return NGX_HTTP_INTERNAL_SERVER_ERROR;
		}

		if (r != r->main && of.size == 0) {
			return ngx_http_send_header(r);
		}

		r->allow_ranges = 1;

		/* we need to allocate all before the header would be sent */

		b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
		if (b == NULL) {
			return NGX_HTTP_INTERNAL_SERVER_ERROR;
		}

		b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
		if (b->file == NULL) {
			return NGX_HTTP_INTERNAL_SERVER_ERROR;
		}

		rc = ngx_http_send_header(r);

		if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
			return rc;
		}

		b->file_pos = 0;
		b->file_last = of.size;

		b->in_file = b->file_last ? 1: 0;
		b->last_buf = (r == r->main) ? 1: 0;
		b->last_in_chain = 1;

		b->file->fd = of.fd;
		b->file->name = path;
		b->file->log = log;
		b->file->directio = of.is_directio;

		out.buf = b;
		out.next = NULL;

		//return ngx_http_output_filter(r, &out);	
		ngx_http_finalize_request(r, ngx_http_output_filter(r, &out));return NGX_DONE;
		/*cerr << req->getUrl() << " " << cntpath << endl; 
		cntpath = cntpath.substr(0, cntpath.length()-1);
		string rurl = req->getUrl();
		StringUtil::replaceAll(cntpath, "//", "/");
		StringUtil::replaceFirst(rurl, cntpath, "");
		cerr << req->getUrl() << " " << cntpath <<  " " << rurl << endl;
		ngx_str_t nuri;
		nuri.data = rurl.c_str();
		nuri.len = ngx_strlen(nuri.data);
		return ngx_http_internal_redirect(r, &nuri, NULL);*/
	}
}