NCBI C++ ToolKit
nc_lib.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: nc_lib.cpp 81992 2018-04-23 17:31:28Z gouriano $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Pavel Ivanov
27  *
28  * File Description: Network cache daemon
29  *
30  */
31 
32 #include "nc_pch.hpp"
33 
34 // Additional sources to avoid linking with related libraries and to force
35 // everybody to use CTaskServer's infrastructure of threads, diagnostics and
36 // application-related stuff
37 
38 // define dummy CThread::GetSelf() -
39 // it is mentioned in random_gen.cpp, but not actually used in netcache
40 namespace CThread {
41 static unsigned int GetSelf() {return 1;}
42 };
43 #include "../../util/random_gen.cpp"
44 
45 #include "../../util/checksum.cpp"
46 #include "../../util/md5.cpp"
47 #include "../../connect/services/netservice_protocol_parser.cpp"
48 #define NO_COMPOUND_ID_SUPPORT
49 #include "../../connect/services/netcache_key.cpp"
50 #undef HAVE_SQLITE3ASYNC_H
51 #undef HAVE_SQLITE3_UNLOCK_NOTIFY
52 #include "../../db/sqlite/sqlitewrapp.cpp"
53 #undef NCBI_USE_ERRCODE_X
54 
55 
57 
58 
59 const char*
61 {
62  switch ( GetErrCode() ) {
63  case eNoInput: return "eNoInput";
64  case eWrongCommand: return "eWrongCommand";
65  case eWrongData: return "eWrongData";
66  default: return CException::GetErrCodeString();
67  }
68 }
69 
70 unsigned g_NumberOfUnderscoresPlusOne(const string& str)
71 {
72  unsigned underscore_count = 1;
73  const char* underscore = strchr(str.c_str(), '_');
74  while (underscore != NULL) {
75  ++underscore_count;
76  underscore = strchr(underscore + 1, '_');
77  }
78  return underscore_count;
79 }
80 
81 // find client IP in HTTP headers
82 // see CCgiRequest::x_SetClientIpProperty(TFlags flags) const
83 string g_GetClientIP( const map<string,string>& headers)
84 {
85  bool internal = headers.find("CAF-INTERNAL") != headers.end();
86  bool external = headers.find("CAF-EXTERNAL") != headers.end();
87  string client;
88  if ( internal || !external ) {
89  if (headers.find("CLIENT-HOST") != headers.end()) {
90  client = headers.at("CLIENT-HOST");
91  }
92  }
93  if (client.empty()) {
94  for( const auto& h : {"CAF-PROXIED-HOST", "X-FORWARDED-FOR", "PROXIED-IP", "X-FWD-IP-ADDR", "X-REAL-IP"}) {
95  if (headers.find(h) != headers.end()) {
96  client = headers.at(h);
97  if (!client.empty()) {
98  break;
99  }
100  }
101  }
102  }
103  return client;
104 }
105 
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
static const char * str(char *buf, int n)
Definition: stats.c:84
#define NULL
Definition: ncbistd.hpp:225
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbiexpt.cpp:453
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static TID GetSelf(void)
Definition: ncbithr.cpp:515
virtual const char * GetErrCodeString(void) const override
Get error code interpreted as text.
Definition: nc_lib.cpp:60
string g_GetClientIP(const map< string, string > &headers)
Definition: nc_lib.cpp:83
unsigned g_NumberOfUnderscoresPlusOne(const string &str)
Definition: nc_lib.cpp:70
static CNamedPipeClient * client
Modified on Fri Sep 20 14:58:15 2024 by modify_doxy.py rev. 669887