Example #1
0
String HTTPSessionList::addNewSession(const String &userName, const String &ip)
{
    PEG_METHOD_ENTER(TRC_AUTHENTICATION, "HTTPSessionList::addNewSession");

    unsigned char raw_id[SESSION_ID_SIZE];

    HTTPSession *session = new HTTPSession(userName, ip);

    String sessionID;
    bool unique;
    // Generate unique and random sessionID.
    do
    {
        // This code assumes that OpenSSL was properly seeded.
        // Now it happens in SSLContext constructor, called by CIMServer
        // constructor.
        int ret = RAND_bytes(raw_id, sizeof(raw_id));
        if (ret != 1)
        {
            // TODO: report error and throw something useful.
        }

        // Convert to a string, that can be transported in HTTP headers
        // (using base64 here).
        Buffer raw_buf((const char*) raw_id, sizeof(raw_id));
        Buffer base64_buf = Base64::encode(raw_buf);
        sessionID.assign(base64_buf.getData(), base64_buf.size());

        {
            AutoMutex lock(_sessionsMutex);
            unique = _sessions.insert(sessionID, session);
        }
    } while (!unique);

    PEG_TRACE((TRC_AUTHENTICATION, Tracer::LEVEL3,
            "Created session %s for user %s@%s",
            (const char *) sessionID.getCString(),
            (const char *) userName.getCString(),
            (const char *) ip.getCString()));


    PEG_METHOD_EXIT();
    return sessionID;
}
Example #2
0
bool zip_archive_impl::read_file_entry(const pstring& entry_name, vector<unsigned char>& buf) const
{
    pstring name(entry_name);
    filename_map_type::const_iterator it = m_filenames.find(name);
    if (it == m_filenames.end())
        // entry name not found.
        return false;

    size_t index = it->second;
    if (index >= m_file_params.size())
        // entry index is out of bound.
        return false;

    const zip_file_param& param = m_file_params[index];

    // Skip the file header section.
    zip_stream_parser file_header(m_stream, param.offset_file_header);
    file_header.skip_bytes(4);
    file_header.skip_bytes(2);
    file_header.skip_bytes(2);
    file_header.skip_bytes(2);
    file_header.skip_bytes(2);
    file_header.skip_bytes(2);
    file_header.skip_bytes(4);
    file_header.skip_bytes(4);
    file_header.skip_bytes(4);
    uint16_t filename_len = file_header.read_2bytes();
    uint16_t extra_field_len = file_header.read_2bytes();
    file_header.skip_bytes(filename_len);
    file_header.skip_bytes(extra_field_len);

    // Data section is immediately followed by the header section.
    m_stream->seek(file_header.tell());

    vector<unsigned char> raw_buf(param.size_compressed+1, 0);
    m_stream->read(&raw_buf[0], param.size_compressed);

    switch (param.compress_method)
    {
        case zip_file_param::stored:
            // Not compressed at all.
            buf.swap(raw_buf);
            return true;
        case zip_file_param::deflated:
        {
            // deflate compression
            vector<unsigned char> zip_buf(param.size_uncompressed+1, 0); // null-terminated
            zip_inflater inflater(raw_buf, zip_buf, param);
            if (!inflater.init())
                break;

            if (!inflater.inflate())
                throw zip_error("error during inflate.");

            buf.swap(zip_buf);
            return true;
        }
        default:
            ;
    }

    return false;
}
Example #3
0
void test_stream_adaptor()
{
    sp_mstream_t sp_stm=memory_stream_t::s_create();
    //pass test,2006-7-24
    //->
    printf("just created,sp_stm->get_ref_cnt:%d\n",sp_stm->get_ref_cnt());
    stream_adaptor_t adp((stream_it *)(memory_stream_t*)sp_stm,STM_ADP_OPT_STANDALONE);
    printf("after attach to adaptor,sp_stm->get_ref_cnt:%d\n",sp_stm->get_ref_cnt());
    //adp.attach((stream_it*)0);
    printf("after clear adaptor,sp_stm->get_ref_cnt:%d\n",sp_stm->get_ref_cnt());

    adp.set_nbo();
    //adp.set_byte_order(BO_UNKNOWN);

    adp<<(int8)(-8)<<(uint8)(8);
    adp<<(int16)(-32767)<<(uint16)(32767);
    adp<<(int32)(-1048576)<<(uint32)(1048576);
    adp<<"hello every one!!!";
    char ca[]= {'A','B','C'};
    adp<<stream_adaptor_t::raw_buf_t(ca,3);

    char cb[]="morining from bb_t!!!";
    bb_t bb;
    bb.fill_from(cb,sizeof(cb),0);
    adp<<bb;

    int8 i8=0;
    uint8 ui8=0;
    int16 i16=0;
    uint16 ui16=0;
    int32 i32=0;
    uint32 ui32=0;
    int8 str[32];
    stream_adaptor_t::raw_str_t raw_str(str,32,true); //len: control if the tail will be truncated
    stream_adaptor_t::raw_buf_t raw_buf(str,32);

    sp_stm->seek(STM_SEEK_BEGIN,0);
    adp>>i8>>ui8;
    printf("read from stream:i8=%d,ui8=%d\n",i8,ui8);

    adp>>i16>>ui16;
    printf("read from stream:i16=%d,ui16=%d\n",i16,ui16);

    adp>>i32>>ui32;
    printf("read from stream:i32=%d,ui32=%d\n",i32,ui32);


    adp>>raw_str;
    printf("read from stream:string=%s\n",str);

    char szTmp[4]= {0};
    stream_adaptor_t::raw_buf_t raw_buf_tmp(szTmp,4);
    adp>>raw_buf_tmp;
    for(int i=0; i<raw_buf_tmp.get_data_size(); ++i) printf("char in szTmp[%d]=%c\n",i,szTmp[i]);

    bb_t bbo;
    adp>>bbo;
    printf("read bb_t from stream:%s,size=%d\n",(int8*)bbo,bbo.get_filled_len());

    //test network byte order--pass test,2006-7-25
    printf("********read from a network byte order with/without transfer to host byte order********\n");
    sp_stm->seek(STM_SEEK_BEGIN,0);
    stream_adaptor_t adp1((stream_it*)(memory_stream_t*)sp_stm,STM_ADP_OPT_STANDALONE);
    adp1.set_nbo();

    adp1>>i8>>ui8;
    printf("read from stream:i8=%d,ui8=%d\n",i8,ui8);

    adp1>>i16>>ui16;
    printf("read from stream:i16=%d,ui16=%d\n",i16,ui16);

    adp1>>i32>>ui32;
    printf("read from stream:i32=%d,ui32=%d\n",i32,ui32);
    //<-
}