Example #1
0
/*----------------------------------------------------------------------
|   PLT_Didl::AppendXmlEscape
+---------------------------------------------------------------------*/
void
PLT_Didl::AppendXmlEscape(NPT_String& out, const char* in)
{
    if (!in) return;

    for (int i=0; i<(int)NPT_StringLength(in); i++) {
        if (*(in+i) == '<') {
            out += "&lt;";
        } else if (*(in+i) == '>') {
            out += "&gt;";
        } else if (*(in+i) == '&') {
            out += "&amp;";
        } else if (*(in+i) == '"') {
            out += "&quot;";
        }  else if (*(in+i) == '\'') {
            out += "&apos;";
        } else {
            out += *(in+i);
        }
    }
}
/*----------------------------------------------------------------------
|   NPT_LogManager::HaveLoggerConfig
+---------------------------------------------------------------------*/
bool
NPT_LogManager::HaveLoggerConfig(const char* name)
{
    NPT_Size name_length = NPT_StringLength(name);
    for (NPT_List<NPT_LogConfigEntry>::Iterator i = m_Config.GetFirstItem();
         i;
         ++i) {
        NPT_LogConfigEntry& entry = *i;
        if (entry.m_Key.StartsWith(name)) {
            const char* suffix = entry.m_Key.GetChars()+name_length;
            if (NPT_StringsEqual(suffix, ".level") ||
                NPT_StringsEqual(suffix, ".handlers") ||
                NPT_StringsEqual(suffix, ".forward")) {
                return true;
            }
        }
    }

    /* no config found */
    return false;
}
Example #3
0
/*----------------------------------------------------------------------
|   NPT_String::ReverseFind
+---------------------------------------------------------------------*/
int
NPT_String::ReverseFind(const char* str, NPT_Ordinal start, bool ignore_case) const
{
    // check args
    if (str == NULL || *str == '\0') return -1;

    // look for a substring
    NPT_Size my_length = GetLength();
    NPT_Size str_length = NPT_StringLength(str);
    int i=my_length-start-str_length;
    const char* src = GetChars();
    if (i<0) return -1;
    for (;i>=0; i--) {
        int cmp = NPT_StringStartsWith(src+i, str, ignore_case);
        if (cmp == 1) {
            // match
            return i;
        }
    }

    return -1;
}
Example #4
0
/*----------------------------------------------------------------------
|   NPT_String::Split
+---------------------------------------------------------------------*/
NPT_List<NPT_String> 
NPT_String::Split(const char* separator) const
{
    NPT_List<NPT_String> result;
    NPT_Size             separator_length = NPT_StringLength(separator);
    
    // sepcial case for empty separators
    if (separator_length == 0) {
        result.Add(*this);
        return result;
    }
    
    int current = 0;  
    int next;  
    do {
        next = Find(separator, current);
        unsigned int end = (next>=0?(unsigned int)next:GetLength());
        result.Add(SubString(current, end-current));
        current = next+separator_length;
    } while (next >= 0);
    
    return result;
}
static int
TestRemoteServer(const char* hostname, unsigned int port, bool verify_cert, NPT_Result expected_cert_verif_result)
{
    printf("[1] Connecting to %s...\n", hostname);
    NPT_Socket* client_socket = new NPT_TcpClientSocket();
    NPT_IpAddress server_ip;
    NPT_Result result = server_ip.ResolveName(hostname);
    if (NPT_FAILED(result)) {
        printf("!ERROR cannot resolve hostname\n");
        return 1;
    }
    NPT_SocketAddress server_addr(server_ip, port);
    result = client_socket->Connect(server_addr);
    printf("[2] Connection result = %d (%s)\n", result, NPT_ResultText(result));
    if (NPT_FAILED(result)) {
        printf("!ERROR\n");
        return 1;
    }
    
    NPT_InputStreamReference input;
    NPT_OutputStreamReference output;
    client_socket->GetInputStream(input);
    client_socket->GetOutputStream(output);
    delete client_socket;
    NPT_TlsContextReference context(new NPT_TlsContext(NPT_TLS_CONTEXT_OPTION_VERIFY_LATER));
    
    /* self-signed cert */
    result = context->LoadKey(NPT_TLS_KEY_FORMAT_PKCS8, TestClient_p8_1, TestClient_p8_1_len, "neptune");
    CHECK(result == NPT_SUCCESS);
    result = context->SelfSignCertificate("MyClientCommonName", "MyClientOrganization", "MyClientOrganizationalName");

    NPT_DataBuffer ta_data;
    NPT_Base64::Decode(EquifaxCA, NPT_StringLength(EquifaxCA), ta_data);
    result = context->AddTrustAnchor(ta_data.GetData(), ta_data.GetDataSize());
    if (NPT_FAILED(result)) {
        printf("!ERROR: context->AddTrustAnchor() \n");
        return 1;
    }
    result = context->AddTrustAnchors(NptTlsDefaultTrustAnchors);
    if (NPT_FAILED(result)) {
        printf("!ERROR: context->AddTrustAnchors() \n");
        return 1;
    }
    NPT_TlsClientSession session(context, input, output);
    printf("[3] Performing Handshake\n");
    result = session.Handshake();
    printf("[4] Handshake Result = %d (%s)\n", result, NPT_ResultText(result));
    if (NPT_FAILED(result)) {
        printf("!ERROR\n");
        return 1;
    }

    PrintSessionInfo(session);
    if (verify_cert) {
        result = session.VerifyPeerCertificate();
        printf("[9] Certificate Verification Result = %d (%s)\n", result, NPT_ResultText(result));
        if (result != expected_cert_verif_result) {
            printf("!ERROR, cert verification expected %d, got %d\n", expected_cert_verif_result, result);
            return 1;
        }
    }
    
    NPT_InputStreamReference  ssl_input;
    NPT_OutputStreamReference ssl_output;
    session.GetInputStream(ssl_input);
    session.GetOutputStream(ssl_output);
    
    printf("[10] Getting / Document\n");
    ssl_output->WriteString("GET / HTTP/1.0\n\n");
    for (;;) {
        unsigned char buffer[1];
        NPT_Size bytes_read = 0;
        result = ssl_input->Read(&buffer[0], 1, &bytes_read);
        if (NPT_SUCCEEDED(result)) {
            CHECK(bytes_read == 1);
            printf("%c", buffer[0]);
        } else {
            if (result != NPT_ERROR_EOS && result != NPT_ERROR_CONNECTION_ABORTED) {
                printf("!ERROR: Read() returned %d (%s)\n", result, NPT_ResultText(result)); 
            }
            break;
        }
    }
    printf("[9] SUCCESS\n");
    
    return 0;
}
Example #6
0
/*----------------------------------------------------------------------
|   NPT_DateTime::FromString
+--------------------------------------------------------------------*/
NPT_Result
NPT_DateTime::FromString(const char* date, Format format)
{
    if (date == NULL || date[0] == '\0') return NPT_ERROR_INVALID_PARAMETERS;
    
    // create a local copy to work with
    NPT_String workspace(date);
    char* input = workspace.UseChars();
    NPT_Size input_size = workspace.GetLength();
    
    switch (format) {
      case FORMAT_W3C: {
        if (input_size < 17 && input_size != 10) return NPT_ERROR_INVALID_SYNTAX;

        // check separators
        if (input[4] != '-' || 
            input[7] != '-') {
            return NPT_ERROR_INVALID_SYNTAX;
        }
         
        // replace separators with terminators
        input[4] = input[7] = '\0';
        
        bool no_seconds = true;
        if (input_size > 10) {
            if (input[10] != 'T' || 
                input[13] != ':') {
                return NPT_ERROR_INVALID_SYNTAX;
            }
           input[10] = input[13] = '\0';
            if (input[16] == ':') {
                input[16] = '\0';
                no_seconds = false;
                if (input_size < 20) return NPT_ERROR_INVALID_SYNTAX;
            } else {
                m_Seconds = 0;
            }
        }
          
    
        // parse CCYY-MM-DD fields
        if (NPT_FAILED(NPT_ParseInteger(input,    m_Year,    false)) ||
            NPT_FAILED(NPT_ParseInteger(input+5,  m_Month,   false)) ||
            NPT_FAILED(NPT_ParseInteger(input+8,  m_Day,     false))) {
            return NPT_ERROR_INVALID_SYNTAX;
        }

        // parse remaining fields if any
        if (input_size > 10) {
            // parse the timezone part
            if (input[input_size-1] == 'Z') {
                m_TimeZone = 0;
                input[input_size-1] = '\0';
            } else if (input[input_size-6] == '+' || input[input_size-6] == '-') {
                if (input[input_size-3] != ':') return NPT_ERROR_INVALID_SYNTAX;
                input[input_size-3] = '\0';
                unsigned int hh, mm;
                if (NPT_FAILED(NPT_ParseInteger(input+input_size-5, hh, false)) ||
                    NPT_FAILED(NPT_ParseInteger(input+input_size-2, mm, false))) {
                    return NPT_ERROR_INVALID_SYNTAX;
                }
                if (hh > 59 || mm > 59) return NPT_ERROR_INVALID_SYNTAX;
                m_TimeZone = hh*60+mm;
                if (input[input_size-6] == '-') m_TimeZone = -m_TimeZone;
                input[input_size-6] = '\0';
            }
            
            // parse fields
            if (NPT_FAILED(NPT_ParseInteger(input+11, m_Hours,   false)) ||
                NPT_FAILED(NPT_ParseInteger(input+14, m_Minutes, false))) {
                return NPT_ERROR_INVALID_SYNTAX;
            }
            if (!no_seconds && input[19] == '.') {
                char fraction[10];
                fraction[9] = '\0';
                unsigned int fraction_size = NPT_StringLength(input+20);
                if (fraction_size == 0) return NPT_ERROR_INVALID_SYNTAX;
                for (unsigned int i=0; i<9; i++) {
                    if (i < fraction_size) {
                        fraction[i] = input[20+i];
                    } else {
                        fraction[i] = '0';
                    }
                }
                if (NPT_FAILED(NPT_ParseInteger(fraction, m_NanoSeconds, false))) {
                    return NPT_ERROR_INVALID_SYNTAX;
                }
                input[19] = '\0';
            } else {
                m_NanoSeconds = 0;
            }
            if (!no_seconds) {
                if (NPT_FAILED(NPT_ParseInteger(input+17, m_Seconds, false))) {
                    return NPT_ERROR_INVALID_SYNTAX;
                }
            }
        }
        break;
      }
    
      case FORMAT_RFC_1036: 
      case FORMAT_RFC_1123: {
        if (input_size < 26) return NPT_ERROR_INVALID_SYNTAX;
        // look for the weekday and separtor
        const char* wday = input;
        while (*input && *input != ',') {
            ++input;
            --input_size;
        }
        if (*input == '\0' || *wday == ',') return NPT_ERROR_INVALID_SYNTAX;
        *input++ = '\0';
        --input_size;
        
        // look for the timezone
        char* timezone = input+input_size-1;
        unsigned int timezone_size = 0;
        while (input_size && *timezone != ' ') {
            --timezone;
            ++timezone_size;
            --input_size;
        }
        if (input_size == 0) return NPT_ERROR_INVALID_SYNTAX;
        *timezone++ = '\0';
        
        // check separators
        if (input_size < 20) return NPT_ERROR_INVALID_SYNTAX;
        unsigned int yl = input_size-18;
        if (yl != 2 && yl != 4) return NPT_ERROR_INVALID_SYNTAX;
        char sep;
        int wday_index;
        if (format == FORMAT_RFC_1036) {
            sep = '-';
            wday_index = MatchString(wday, NPT_TIME_DAYS_LONG, 7);
        } else {
            sep = ' ';
            wday_index = MatchString(wday, NPT_TIME_DAYS_SHORT, 7);
        }
        if (input[0]     != ' ' || 
            input[3]     != sep || 
            input[7]     != sep ||
            input[8+yl]  != ' ' ||
            input[11+yl] != ':' ||
            input[14+yl] != ':') {
            return NPT_ERROR_INVALID_SYNTAX;
        }
        input[3] = input[7] = input[8+yl] = input[11+yl] = input[14+yl] = '\0';            

        // parse fields
        m_Month = 1+MatchString(input+4, NPT_TIME_MONTHS, 12);
        if (NPT_FAILED(NPT_ParseInteger(input+1,     m_Day,     false)) ||
            NPT_FAILED(NPT_ParseInteger(input+8,     m_Year,    false)) ||
            NPT_FAILED(NPT_ParseInteger(input+9+yl,  m_Hours,   false)) ||
            NPT_FAILED(NPT_ParseInteger(input+12+yl, m_Minutes, false)) ||
            NPT_FAILED(NPT_ParseInteger(input+15+yl, m_Seconds, false))) {
            return NPT_ERROR_INVALID_SYNTAX;
        }
        
        // adjust short year lengths
        if (yl == 2) m_Year += 1900;
        
        // parse the timezone
        if (NPT_StringsEqual(timezone, "GMT") ||
            NPT_StringsEqual(timezone, "UT")  ||
            NPT_StringsEqual(timezone, "Z")) {
            m_TimeZone = 0;
        } else if (NPT_StringsEqual(timezone, "EDT")) {
            m_TimeZone = -4*60;
        } else if (NPT_StringsEqual(timezone, "EST") ||
                   NPT_StringsEqual(timezone, "CDT")) {
            m_TimeZone = -5*60;
        } else if (NPT_StringsEqual(timezone, "CST") ||
                   NPT_StringsEqual(timezone, "MDT")) {
            m_TimeZone = -6*60;
        } else if (NPT_StringsEqual(timezone, "MST") ||
                   NPT_StringsEqual(timezone, "PDT")) {
            m_TimeZone = -7*60;
        } else if (NPT_StringsEqual(timezone, "PST")) {
            m_TimeZone = -8*60;
        } else if (timezone_size == 1) {
            if (timezone[0] >= 'A' && timezone[0] <= 'I') {
                m_TimeZone = -60*(1+timezone[0]-'A');
            } else if (timezone[0] >= 'K' && timezone[0] <= 'M') {
                m_TimeZone = -60*(timezone[0]-'A');            
            } else if (timezone[0] >= 'N' && timezone[0] <= 'Y') {
                m_TimeZone = 60*(1+timezone[0]-'N');
            } else {
                return NPT_ERROR_INVALID_SYNTAX;
            }
        } else if (timezone_size == 5) {
            int sign;
            if (timezone[0] == '-') {
                sign = -1;
            } else if (timezone[0] == '+') {
                sign = 1;
            } else {
                return NPT_ERROR_INVALID_SYNTAX;
            }
            NPT_UInt32 tz;
            if (NPT_FAILED(NPT_ParseInteger(timezone+1, tz, false))) {
                return NPT_ERROR_INVALID_SYNTAX;
            }
            unsigned int hh = (tz/100);
            unsigned int mm = (tz%100);
            if (hh > 59 || mm > 59) return NPT_ERROR_INVALID_SYNTAX;
            m_TimeZone = sign*(hh*60+mm);
        } else {
            return NPT_ERROR_INVALID_SYNTAX;
        }
        
        // compute the number of days elapsed since 1900
        NPT_UInt32 days = ElapsedDaysSince1900(*this);
        if ((int)((days+1)%7) != wday_index) {
            return NPT_ERROR_INVALID_PARAMETERS;
        }
        
        m_NanoSeconds = 0;

        break;
      }
    
      case FORMAT_ANSI: {
        if (input_size != 24) return NPT_ERROR_INVALID_SYNTAX;

        // check separators
        if (input[3]  != ' ' || 
            input[7]  != ' ' || 
            input[10] != ' ' || 
            input[13] != ':' || 
            input[16] != ':' ||
            input[19] != ' ') {
            return NPT_ERROR_INVALID_SYNTAX;
        }
        input[3] = input[7] = input[10] = input[13] = input[16] = input[19] = '\0';
        if (input[8] == ' ') input[8] = '0';
                
        m_Month = 1+MatchString(input+4, NPT_TIME_MONTHS, 12);
        if (NPT_FAILED(NPT_ParseInteger(input+8,  m_Day,     false)) ||
            NPT_FAILED(NPT_ParseInteger(input+11, m_Hours,   false)) ||
            NPT_FAILED(NPT_ParseInteger(input+14, m_Minutes, false)) ||
            NPT_FAILED(NPT_ParseInteger(input+17, m_Seconds, false)) ||
            NPT_FAILED(NPT_ParseInteger(input+20, m_Year,    false))) {
            return NPT_ERROR_INVALID_SYNTAX;
        }

        // compute the number of days elapsed since 1900
        NPT_UInt32 days = ElapsedDaysSince1900(*this);
        if ((int)((days+1)%7) != MatchString(input, NPT_TIME_DAYS_SHORT, 7)) {
            return NPT_ERROR_INVALID_PARAMETERS;
        }
        
        m_TimeZone    = 0;
        m_NanoSeconds = 0;
        break;
      }
    
      default:
        return NPT_ERROR_INVALID_PARAMETERS;
    }
    
    return CheckDate(*this);
}
/*----------------------------------------------------------------------
|   NPT_Log::FormatRecordToStream
+---------------------------------------------------------------------*/
void
NPT_Log::FormatRecordToStream(const NPT_LogRecord& record,
                              NPT_OutputStream&    stream,
                              bool                 use_colors,
                              NPT_Flags            format_filter)
{
    const char* level_name = GetLogLevelName(record.m_Level);
    NPT_String  level_string;

    /* format and emit the record */
    if (level_name[0] == '\0') {
        level_string = NPT_String::FromInteger(record.m_Level);
        level_name = level_string;
    }
    if ((format_filter & NPT_LOG_FORMAT_FILTER_NO_SOURCE) == 0) {
        unsigned int start = 0;
        /* remove source file path if requested */
        if (format_filter & NPT_LOG_FORMAT_FILTER_NO_SOURCEPATH) {
            for (start = NPT_StringLength(record.m_SourceFile);
                 start;
                 --start) {
                if (record.m_SourceFile[start-1] == '\\' ||
                    record.m_SourceFile[start-1] == '/') {
                    break;
                }
            }
        }
        stream.WriteString(record.m_SourceFile + start);
        stream.Write("(", 1, NULL);
        stream.WriteString(NPT_String::FromIntegerU(record.m_SourceLine));
        stream.Write("): ", 3, NULL);
    }
    if ((format_filter & NPT_LOG_FORMAT_FILTER_NO_LOGGER_NAME) == 0) {
        stream.Write("[", 1, NULL);
        stream.WriteString(record.m_LoggerName);
        stream.Write("] ", 2, NULL);
    }
    if ((format_filter & NPT_LOG_FORMAT_FILTER_NO_TIMESTAMP) == 0) {
        NPT_String ts = NPT_DateTime(record.m_TimeStamp, true).ToString(NPT_DateTime::FORMAT_W3C, 
                                                                        NPT_DateTime::FLAG_EMIT_FRACTION | 
                                                                        NPT_DateTime::FLAG_EXTENDED_PRECISION);
        stream.WriteString(ts.GetChars());
        stream.Write(" ", 1);
    }
    if ((format_filter & NPT_LOG_FORMAT_FILTER_NO_FUNCTION_NAME) == 0) {
        stream.WriteFully("[",1);
        if (record.m_SourceFunction) {
            stream.WriteString(record.m_SourceFunction);
        }
        stream.WriteFully("] ",2);
    }
    if ((format_filter & NPT_LOG_FORMAT_FILTER_NO_THREAD_ID) == 0) {
        stream.Write("(", 1, NULL);
        stream.WriteString(NPT_String::FromIntegerU(record.m_ThreadId));
        stream.Write(") ", 2, NULL);
    }
    const char* ansi_color = NULL;
    if (use_colors) {
        ansi_color = GetLogLevelAnsiColor(record.m_Level);
        if (ansi_color) {
            stream.Write("\033[", 2, NULL);
            stream.WriteString(ansi_color);
            stream.Write(";1m", 3, NULL);
        }
    }
    stream.WriteString(level_name);
    if (use_colors && ansi_color) {
        stream.Write("\033[0m", 4, NULL);
    }
    stream.Write(": ", 2, NULL);
    stream.WriteString(record.m_Message);
    stream.Write("\r\n", 2, NULL);
}