<commit_before>/*
 *  Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Contact: <NAME> <<EMAIL>>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
/*
 * @file        cookie.cpp
 * @author      <NAME> (<EMAIL>)
 * @version     1.0
 * @brief       This function contain implementation of CookieService
 */

#include <memory>
#include <dpl/log/log.h>
#include <dpl/serialization.h>
#include <protocols.h>
#include <security-server.h>
#include <security-server-common.h>
#include <cookie.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/smack.h>

//interfaces ID
const int INTERFACE_GET = 0;
const int INTERFACE_CHECK = 1;
const int INTERFACE_CHECK_TMP = 3;

namespace SecurityServer {

GenericSocketService::ServiceDescriptionVector CookieService::GetServiceDescription() {
    ServiceDescription sd1 = {
        "security-server::api-cookie-get",
        INTERFACE_GET,
        SERVICE_SOCKET_COOKIE_GET
    };
    ServiceDescription sd2 = {
        "security-server::api-cookie-check",
        INTERFACE_CHECK,
        SERVICE_SOCKET_COOKIE_CHECK
    };
    ServiceDescription sd3 = {
        "security-server::api-cookie-check",
        INTERFACE_CHECK_TMP,
        SERVICE_SOCKET_COOKIE_CHECK_TMP
    };
    ServiceDescriptionVector v;
    v.push_back(sd1);
    v.push_back(sd2);
    v.push_back(sd3);
    return v;
}

void CookieService::accept(const AcceptEvent &event) {
    LogDebug("Accept event. ConnectionID.sock: " << event.connectionID.sock
        << " ConnectionID.counter: " << event.connectionID.counter
        << " ServiceID: " << event.interfaceID);
    auto &info = m_socketInfoMap[event.connectionID.counter];
    info.interfaceID = event.interfaceID;
}

void CookieService::write(const WriteEvent &event) {
    LogDebug("WriteEvent. ConnectionID: " << event.connectionID.sock <<
        " Size: " << event.size << " Left: " << event.left);
    if (event.left == 0)
        m_serviceManager->Close(event.connectionID);
}

void CookieService::read(const ReadEvent &event) {
    LogDebug("Read event for counter: " << event.connectionID.counter);
    auto &info = m_socketInfoMap[event.connectionID.counter];
    info.buffer.Push(event.rawBuffer);

    // We can get several requests in one package.
    // Extract and process them all
    while(readOne(event.connectionID, info.buffer, info.interfaceID));
}

void CookieService::close(const CloseEvent &event) {
    LogDebug("CloseEvent. ConnectionID: " << event.connectionID.sock);
    m_socketInfoMap.erase(event.connectionID.counter);
}

void CookieService::error(const ErrorEvent &event) {
    LogDebug("ErrorEvent. ConnectionID: " << event.connectionID.sock);
    m_serviceManager->Close(event.connectionID);
}

bool CookieService::readOne(const ConnectionID &conn, SocketBuffer &buffer, int interfaceID)
{
    LogDebug("Iteration begin");
    SocketBuffer send, recv;
    int msgType;
    bool removeGarbage = false;

    //waiting for all data
    if (!buffer.Ready()) {
        return false;
    }

    //receive data from buffer and check MSG_ID
    Try {
        Deserialization::Deserialize(buffer, msgType);  //receive MSG_ID
    } Catch (SocketBuffer::Exception::Base) {
        LogDebug("Broken protocol. Closing socket.");
        m_serviceManager->Close(conn);
        return false;
    }

    bool retval = false;

    //use received data
    if (interfaceID == INTERFACE_GET) {
        switch(msgType) {<commit_msg>Making security-server buildable under GCC 4.8.<commit_after>/*
 *  Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Contact: <NAME> <<EMAIL>>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
/*
 * @file        cookie.cpp
 * @author      <NAME> (<EMAIL>)
 * @version     1.0
 * @brief       This function contain implementation of CookieService
 */

#include <memory>
#include <dpl/log/log.h>
#include <dpl/serialization.h>
#include <protocols.h>
#include <security-server.h>
#include <security-server-common.h>
#include <cookie.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/smack.h>

//interfaces ID
const int INTERFACE_GET = 0;
const int INTERFACE_CHECK = 1;
const int INTERFACE_CHECK_TMP = 3;

namespace SecurityServer {

GenericSocketService::ServiceDescriptionVector CookieService::GetServiceDescription() {
    ServiceDescription sd1 = {
        "security-server::api-cookie-get",
        INTERFACE_GET,
        SERVICE_SOCKET_COOKIE_GET
    };
    ServiceDescription sd2 = {
        "security-server::api-cookie-check",
        INTERFACE_CHECK,
        SERVICE_SOCKET_COOKIE_CHECK
    };
    ServiceDescription sd3 = {
        "security-server::api-cookie-check",
        INTERFACE_CHECK_TMP,
        SERVICE_SOCKET_COOKIE_CHECK_TMP
    };
    ServiceDescriptionVector v;
    v.push_back(sd1);
    v.push_back(sd2);
    v.push_back(sd3);
    return v;
}

void CookieService::accept(const AcceptEvent &event) {
    LogDebug("Accept event. ConnectionID.sock: " << event.connectionID.sock
        << " ConnectionID.counter: " << event.connectionID.counter
        << " ServiceID: " << event.interfaceID);
    auto &info = m_socketInfoMap[event.connectionID.counter];
    info.interfaceID = event.interfaceID;
}

void CookieService::write(const WriteEvent &event) {
    LogDebug("WriteEvent. ConnectionID: " << event.connectionID.sock <<
        " Size: " << event.size << " Left: " << event.left);
    if (event.left == 0)
        m_serviceManager->Close(event.connectionID);
}

void CookieService::read(const ReadEvent &event) {
    LogDebug("Read event for counter: " << event.connectionID.counter);
    auto &info = m_socketInfoMap[event.connectionID.counter];
    info.buffer.Push(event.rawBuffer);

    // We can get several requests in one package.
    // Extract and process them all
    while(readOne(event.connectionID, info.buffer, info.interfaceID));
}

void CookieService::close(const CloseEvent &event) {
    LogDebug("CloseEvent. ConnectionID: " << event.connectionID.sock);
    m_socketInfoMap.erase(event.connectionID.counter);
}

void CookieService::error(const ErrorEvent &event) {
    LogDebug("ErrorEvent. ConnectionID: " << event.connectionID.sock);
    m_serviceManager->Close(event.connectionID);
}

bool CookieService::readOne(const ConnectionID &conn, SocketBuffer &buffer, int interfaceID)
{
    LogDebug("Iteration begin");
    SocketBuffer send, recv;
    int msgType;
    bool removeGarbage = false;

    //waiting for all data
    if (!buffer.Ready()) {
        return false;
    }

    //receive data from buffer and check MSG_ID
    Try {
        Deserialization::Deserialize(buffer, msgType);  //receive MSG_ID
    } Catch (SocketBuffer::Exception::Base) {
        LogDebug("Broken protocol. Closing socket.");
        m_serviceManager->Close(conn);
        return false;
    }

    bool retval = false;

    //use received data
    if (interfaceID == INTERFACE_GET) {
        switch(static_cast<CookieCall>(msgType)) {
{'fraction_non_alphanumeric': 0.11491809428535701, 'fraction_numerical': 0.006252344629235964, 'mean_word_length': 3.485698261357263, 'pattern_counts': {'":': 0, '<': 86, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7196712', 'n_tokens_mistral': 2441, 'n_tokens_neox': 2371, 'n_words': 607}