示例#1
0
文件: connection.cpp 项目: sbunce/p2p
void connection::recv_call_back(net::connection_info & CI)
{
	namespace fs = boost::filesystem;

	//check if exceeded max request size
	if(CI.recv_buf.size() > 1024){
		Proactor.disconnect(CI.connection_ID);
		return;
	}

	//sub-expression 1 is the request path
	boost::regex expression("GET\\s([^\\s]*)(\\s|\\r|\\n)");
	boost::match_results<std::string::iterator> what;
	std::string req = CI.recv_buf.str();
	if(boost::regex_search(req.begin(), req.end(), what, expression)){
		try{
			std::string temp = what[1];
			decode_chars(temp);
			LOG << "request: " << temp;
			path = fs::system_complete(fs::path(web_root + temp, fs::native));
			if(path.string().find("..") != std::string::npos){
				//stop directory traversal
				CI.recv_call_back.clear();
				Proactor.disconnect(CI.connection_ID);
			}else{
				if(fs::exists(path)){
					if(fs::is_directory(path)){
						//check if there is a index file in the directory
						fs::path temp = fs::system_complete(fs::path(path.string()
							+ "/index.html", fs::native));
						if(fs::exists(temp)){
							//serve the index file
							path = temp;
							CI.recv_call_back.clear();
							CI.send_call_back = boost::bind(&connection::file_send_call_back, this, _1);
							CI.send_call_back(CI);
						}else{
							//serve directory listing
							CI.recv_call_back.clear();
							read_directory(CI);
						}
					}else{
						//serve file
						CI.recv_call_back.clear();
						CI.send_call_back = boost::bind(&connection::file_send_call_back, this, _1);
						CI.send_call_back(CI);
					}
				}else{
					//file at request path doesn't exist
					net::buffer B("404");
					Proactor.send(CI.connection_ID, B);
					Proactor.disconnect_on_empty(CI.connection_ID);
				}
			}
		}catch(const std::exception & e){
			//generally happens if path too long
			LOG << e.what();
			net::buffer B("404");
			Proactor.send(CI.connection_ID, B);
			Proactor.disconnect_on_empty(CI.connection_ID);
			return;
		}
	}
}
示例#2
0
void
ssa_parser_c::add_attachment_maybe(std::string &name,
                                   std::string &data_uu,
                                   ssa_section_e section) {
    if (name.empty() || data_uu.empty() || ((SSA_SECTION_FONTS != section) && (SSA_SECTION_GRAPHICS != section))) {
        name    = "";
        data_uu = "";
        return;
    }

    ++m_attachment_id;

    if (!m_reader->attachment_requested(m_attachment_id)) {
        name    = "";
        data_uu = "";
        return;
    }

    attachment_t attachment;

    std::string short_name = m_file_name;
    size_t pos             = short_name.rfind('/');

    if (std::string::npos != pos)
        short_name.erase(0, pos + 1);
    pos = short_name.rfind('\\');
    if (std::string::npos != pos)
        short_name.erase(0, pos + 1);

    attachment.ui_id        = m_attachment_id;
    attachment.name         = m_cc_utf8->utf8(name);
    attachment.description  = (boost::format(SSA_SECTION_FONTS == section ? Y("Imported font from %1%") : Y("Imported picture from %1%")) % short_name).str();
    attachment.to_all_files = true;

    size_t allocated        = 1024;
    attachment.data         = memory_c::alloc(allocated);
    attachment.data->set_size(0);

    const unsigned char *p  = (const unsigned char *)data_uu.c_str();
    for (pos = 0; data_uu.length() > (pos + 4); pos += 4)
        decode_chars(p[pos], p[pos + 1], p[pos + 2], p[pos + 3], attachment.data, 3, allocated);

    switch (data_uu.length() % 4) {
    case 2:
        decode_chars(p[pos], p[pos + 1], 0, 0, attachment.data, 1, allocated);
        break;
    case 3:
        decode_chars(p[pos], p[pos + 1], p[pos + 2], 0, attachment.data, 2, allocated);
        break;
    }

    attachment.mime_type = guess_mime_type(name, false);

    if (attachment.mime_type == "")
        attachment.mime_type = "application/octet-stream";

    add_attachment(attachment);

    name    = "";
    data_uu = "";
}