<commit_before>// $Id$
// Author: <NAME>   21/12/2013

/*************************************************************************
 * Copyright (C) 1995-2013, <NAME> and <NAME>.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "TCivetweb.h"

#include "../civetweb/civetweb.h"

#include <stdlib.h>
#include <string.h>

#include "THttpServer.h"
#include "THttpWSEngine.h"
#include "TUrl.h"

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TCivetwebWSEngine                                                    //
//                                                                      //
// Implementation of THttpWSEngine for Civetweb                         //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

class TCivetwebWSEngine : public THttpWSEngine {
protected:
   struct mg_connection *fWSconn;

public:
   TCivetwebWSEngine(const char *name, const char *title, struct mg_connection *conn)
      : THttpWSEngine(name, title), fWSconn(conn)
   {
   }

   virtual ~TCivetwebWSEngine() {}

   virtual UInt_t GetId() const { return TString::Hash((void *)fWSconn, sizeof(void *)); }

   virtual void ClearHandle() { fWSconn = 0; }

   virtual void Send(const void *buf, int len)
   {
      if (fWSconn)
         mg_websocket_write(fWSconn, WEBSOCKET_OPCODE_TEXT, (const char *)buf, len);
   }
};

//////////////////////////////////////////////////////////////////////////

int websocket_connect_handler(const struct mg_connection *conn, void *)
{
   const struct mg_request_info *request_info = mg_get_request_info(conn);
   if (request_info == 0)
      return 1;

   TCivetweb *engine = (TCivetweb *)request_info->user_data;
   if (engine == 0)
      return 1;
   THttpServer *serv = engine->GetServer();
   if (serv == 0)
      return 1;

   THttpCallArg arg;
   arg.SetPathAndFileName(request_info->uri); // path and file name
   arg.SetQuery(request_info->query_string);  // query arguments
   arg.SetWSId(TString::Hash((void *)conn, sizeof(void *)));
   arg.SetMethod("WS_CONNECT");

   Bool_t execres = serv->ExecuteHttp(&arg);

   return execres && !arg.Is404() ? 0 : 1;
}

//////////////////////////////////////////////////////////////////////////

void websocket_ready_handler(struct mg_connection *conn, void *)
{
   const struct mg_request_info *request_info = mg_get_request_info(conn);

   TCivetweb *engine = (TCivetweb *)request_info->user_data;
   if (engine == 0)
      return;
   THttpServer *serv = engine->GetServer();
   if (serv == 0)
      return;

   THttpCallArg arg;
   arg.SetPathAndFileName(request_info->uri); // path and file name
   arg.SetQuery(request_info->query_string);  // query arguments
   arg.SetMethod("WS_READY");

   arg.SetWSId(TString::Hash((void *)conn, sizeof(void *)));
   arg.SetWSHandle(new TCivetwebWSEngine("websocket", "title", conn));

   serv->ExecuteHttp(&arg);
}

//////////////////////////////////////////////////////////////////////////

int websocket_data_handler(struct mg_connection *conn, int, char *data, size_t len, void *)
{
   const struct mg_request_info *request_info = mg_get_request_info(conn);

   // do not handle empty data
   if (len == 0)
      return 1;

   TCivetweb *engine = (TCivetweb *)request_info->user_data;
   if (engine == 0)
      return 1;
   THttpServer *serv = engine->GetServer();
   if (serv == 0)
      return 1;

   // seems to be, appears when connection is broken
   if ((len==2) && ((int)data[0] == 3) && ((int)data[1] == -23)) return 0;

   THttpCallArg arg;
   arg.SetPathAndFileName(request_info->uri); // path and file name
   arg.SetQuery(request_info->query_string);  // query arguments
   arg.SetWSId(TString::Hash((void *)conn, sizeof(void *)));
   arg.SetMethod("WS_DATA");

   arg.SetPostData(data, len, kTRUE); // make copy of original data

   serv->ExecuteHttp(&arg);

   return 1;
}

//////////////////////////////////////////////////////////////////////////

void websocket_close_handler(const struct mg_connection *conn, void *)
{<commit_msg>http: clanf-format for broken websocket workaround<commit_after>// $Id$
// Author: <NAME>   21/12/2013

/*************************************************************************
 * Copyright (C) 1995-2013, <NAME> and <NAME>.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "TCivetweb.h"

#include "../civetweb/civetweb.h"

#include <stdlib.h>
#include <string.h>

#include "THttpServer.h"
#include "THttpWSEngine.h"
#include "TUrl.h"

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TCivetwebWSEngine                                                    //
//                                                                      //
// Implementation of THttpWSEngine for Civetweb                         //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

class TCivetwebWSEngine : public THttpWSEngine {
protected:
   struct mg_connection *fWSconn;

public:
   TCivetwebWSEngine(const char *name, const char *title, struct mg_connection *conn)
      : THttpWSEngine(name, title), fWSconn(conn)
   {
   }

   virtual ~TCivetwebWSEngine() {}

   virtual UInt_t GetId() const { return TString::Hash((void *)fWSconn, sizeof(void *)); }

   virtual void ClearHandle() { fWSconn = 0; }

   virtual void Send(const void *buf, int len)
   {
      if (fWSconn)
         mg_websocket_write(fWSconn, WEBSOCKET_OPCODE_TEXT, (const char *)buf, len);
   }
};

//////////////////////////////////////////////////////////////////////////

int websocket_connect_handler(const struct mg_connection *conn, void *)
{
   const struct mg_request_info *request_info = mg_get_request_info(conn);
   if (request_info == 0)
      return 1;

   TCivetweb *engine = (TCivetweb *)request_info->user_data;
   if (engine == 0)
      return 1;
   THttpServer *serv = engine->GetServer();
   if (serv == 0)
      return 1;

   THttpCallArg arg;
   arg.SetPathAndFileName(request_info->uri); // path and file name
   arg.SetQuery(request_info->query_string);  // query arguments
   arg.SetWSId(TString::Hash((void *)conn, sizeof(void *)));
   arg.SetMethod("WS_CONNECT");

   Bool_t execres = serv->ExecuteHttp(&arg);

   return execres && !arg.Is404() ? 0 : 1;
}

//////////////////////////////////////////////////////////////////////////

void websocket_ready_handler(struct mg_connection *conn, void *)
{
   const struct mg_request_info *request_info = mg_get_request_info(conn);

   TCivetweb *engine = (TCivetweb *)request_info->user_data;
   if (engine == 0)
      return;
   THttpServer *serv = engine->GetServer();
   if (serv == 0)
      return;

   THttpCallArg arg;
   arg.SetPathAndFileName(request_info->uri); // path and file name
   arg.SetQuery(request_info->query_string);  // query arguments
   arg.SetMethod("WS_READY");

   arg.SetWSId(TString::Hash((void *)conn, sizeof(void *)));
   arg.SetWSHandle(new TCivetwebWSEngine("websocket", "title", conn));

   serv->ExecuteHttp(&arg);
}

//////////////////////////////////////////////////////////////////////////

int websocket_data_handler(struct mg_connection *conn, int, char *data, size_t len, void *)
{
   const struct mg_request_info *request_info = mg_get_request_info(conn);

   // do not handle empty data
   if (len == 0)
      return 1;

   TCivetweb *engine = (TCivetweb *)request_info->user_data;
   if (engine == 0)
      return 1;
   THttpServer *serv = engine->GetServer();
   if (serv == 0)
      return 1;

   // seems to be, appears when connection is broken
   if ((len == 2) && ((int)data[0] == 3) && ((int)data[1] == -23))
      return 0;

   THttpCallArg arg;
   arg.SetPathAndFileName(request_info->uri); // path and file name
   arg.SetQuery(request_info->query_string);  // query arguments
   arg.SetWSId(TString::Hash((void *)conn, sizeof(void *)));
   arg.SetMethod("WS_DATA");

   arg.SetPostData(data, len, kTRUE); // make copy of original data

   serv->ExecuteHttp(&arg);

   return 1;
}

//////////////////////////////////////////////////////////////////////////

void websocket_close_handler(const struct mg_connection *conn, void *)
{
{'fraction_non_alphanumeric': 0.2381319528582443, 'fraction_numerical': 0.009692697433638067, 'mean_word_length': 1.7573641056787124, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1045073', 'n_tokens_mistral': 2625, 'n_tokens_neox': 2472, 'n_words': 578}