Example #1
0
bool NetChannel::CopyFileFragments()
{
	fragbuf_t *p;
	fragbuf_s *n;
	char filename[MAX_PATH];
	int totalSize = 0;

	if (!m_incomingbufs[FRAG_FILE_STREAM]) {
		m_System->DPrintf("WARNING! NetChannel::CopyFileFragments: called with no fragments readied.\n");
		return false;
	}

	totalSize = 0;
	p = m_incomingbufs[FRAG_FILE_STREAM];
	while (p)
	{
		totalSize += p->size;
		p = p->next;
	}

	BitBuffer filecontent(totalSize);
	p = m_incomingbufs[FRAG_FILE_STREAM];
	while (p)
	{
		n = p->next;
		filecontent.WriteBuf(p->data, p->size);
		Mem_Free(p);
		p = n;
	}

	filecontent.Reset();
	strcopy(filename, filecontent.ReadString());

	if (!strlen(filename)) {
		m_System->Printf("File fragment received with no filename\n");
		FlushIncoming(FRAG_FILE_STREAM);
		return false;
	}

	if (strstr(filename, "..")) {
		m_System->Printf("File fragment received with relative path, ignoring\n");
		FlushIncoming(FRAG_FILE_STREAM);
		return false;
	}

	// TODO: Here is the missing code.
	// TODO: Check me, value of return function only false.

	totalSize -= strlen(filename) - 1;
	m_incomingbufs[FRAG_FILE_STREAM] = nullptr;

	return false;
}
int send_response(http_response_t *response,char *path[], int newfd)
{
	FILE *newfp, *file;
	newfp = fdopen(newfd, "w");
	if(newfp== NULL)
		perror("creating file");
	int i =0;
	fprintf(newfp,"HTTP/%d.%d %d %s\r\n",response->major_version,response->minor_version,response->status.code,response->status.reason);


	for(i=0;i<=3;i++)
	{
		fprintf(newfp,"%s%s\r\n",response->headers[i].field_name,response->headers[i].field_value);
	}
	fprintf(newfp,"\n");

	filecontent(path,newfp);
	fclose(newfp);
	return 0;
}
Example #3
0
void BaseServer::read_callback(bufferevent *pBufferEvent, void *user_data)
{

    evbuffer* inputbuf = bufferevent_get_input(pBufferEvent);
    evbuffer* outputbuf = bufferevent_get_output(pBufferEvent);
    size_t t;
    std::string request;
    request=evbuffer_readln(inputbuf,&t,EVBUFFER_EOL_CRLF);
    printf("request= %s\n",request.c_str());
    std::vector<std::string> result;
    result = split(request," ");

    std::string http_method = result[0];
    std::string http_path   = result[1];
    std::string http_version= result[2];

    std::cout<<"method: "<<http_method<<std::endl;
    std::cout<<"path: "<<http_path<<std::endl;
    std::cout<<"http_version: "<<http_version<<std::endl;

    printf("path=%d\n",http_path.size());

    std::string content_type;
    auto pos = http_path.find(".jpg");
    if(std::string::npos==pos)
        content_type = "text/html\r\n";
    else
        content_type = "image/jpg\r\n";

    if(http_path=="/")
    {
        http_path="index.html";
    }
    else
    {
        http_path=http_path.substr(1);

    }


    std::ifstream ifile(http_path.c_str());
    if(ifile)
    {
        std::stringstream stream;
        stream << ifile.rdbuf();
        std::string filecontent(stream.str());
        ifile.close();
        stream.str("");
        stream<<"HTTP/1.1 200 OK\r\n"<<"Content-Type: "<<content_type
             <<"Content-Length: "<<filecontent.size()<<"\r\n\r\n";
        stream<<filecontent;
        std::string response(stream.str());
        stream.str("");
        //std::cout<<"response****"<<response;
        evbuffer_add(outputbuf,response.c_str(),response.size());
    }else
    {

    }




    evbuffer* tmp=evbuffer_new();
    evbuffer_add_buffer(tmp,inputbuf);
    evbuffer_free(tmp);




}