Skip to content

Commit

Permalink
various enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanocasazza committed Sep 21, 2012
1 parent f5aa7c1 commit aaaaa6e
Show file tree
Hide file tree
Showing 38 changed files with 3,637 additions and 1,890 deletions.
2,322 changes: 1,521 additions & 801 deletions examples/WiAuth/wi_auth.cpp

Large diffs are not rendered by default.

2,322 changes: 1,521 additions & 801 deletions examples/WiAuth/wi_auth.usp

Large diffs are not rendered by default.

74 changes: 28 additions & 46 deletions include/ulib/cache.h
Expand Up @@ -14,7 +14,7 @@
#ifndef ULIB_CACHE_H
#define ULIB_CACHE_H 1

#include <ulib/string.h>
#include <ulib/file.h>

/**
@class UCache
Expand Down Expand Up @@ -59,67 +59,45 @@ class U_EXPORT UCache {
UCache()
{
U_TRACE_REGISTER_OBJECT(0, UCache, "")
}

~UCache()
{
U_TRACE_UNREGISTER_OBJECT(0, UCache)
}

// INIT

void set(char* ptr)
{
U_TRACE(0, "UCache::set(%p)", ptr)

x = ptr + sizeof(UCache::cache_info);
info = (cache_info*)ptr;
}

void initStart()
{
U_TRACE(0, "UCache::initStart()")

u_gettimeofday();

start = u_now->tv_sec;
fd = -1;
}

void init(uint32_t size);
~UCache();

// OPEN/CREAT a cache file

bool open(const UString& path, uint32_t size, const UString* environment = 0);
bool open(const UString& path, uint32_t size, const UString* environment = 0);
bool open(const UString& path, const UString& dir_template, const UString* environment = 0);

// OPERATION

void add(const UString& _key, const UString& _data, uint32_t _ttl = 0)
uint32_t getHSize(uint32_t size) const
{
U_TRACE(1, "UCache::add(%.*S,%.*S,%u)", U_STRING_TO_TRACE(_key), U_STRING_TO_TRACE(_data), _ttl)
U_TRACE(0, "UCache::getHSize(%u)", size)

// sizeof(uint32_t) <= hsize <= size / sizeof(cache_hash_table_entry) (hsize is a power of 2)

const char* key = _key.data();
const char* data = _data.data();
uint32_t keylen = _key.size(),
datalen = _data.size();
uint32_t hsize = sizeof(uint32_t);

char* ptr = add(key, keylen, datalen, _ttl);
while (hsize <= (size / sizeof(UCache::cache_hash_table_entry))) hsize <<= 1U;

(void) u__memcpy(ptr, key, keylen);
(void) u__memcpy(ptr + keylen, data, datalen);
U_RETURN(hsize);
}

// operator []
void add( const UString& key, const UString& data, uint32_t _ttl = 0);
void addContent(const UString& key, const UString& content, uint32_t _ttl = 0); // NB: + null terminator...

UString operator[](const UString& key);
UString get( const char* key, uint32_t len);
UString getContent(const char* key, uint32_t len); // NB: - null terminator...

// SERVICES
void loadContentOf(const UString& directory, const char* filter = 0, uint32_t filter_len = 0);

// operator []

UString contentOf(const char* fmt, ...);
UString contentOf(const UString& pathname);
UString addContentOf(const UString& pathname);
void loadContentOf(const UString& directory, const char* filter = 0, uint32_t filter_len = 0);
UString operator[](const UString& key) { return get(U_STRING_TO_PARAM(key)); }

void reset() { (void) memset(x, 0, info->size); }
// SERVICES

uint32_t getTTL() const
{
Expand Down Expand Up @@ -168,7 +146,8 @@ class U_EXPORT UCache {
// ------> datalen array of char...
} cache_hash_table_entry;

char* x; // cache pointer
int fd;
char* x; // cache pointer
cache_info* info; // cache info pointer
time_t start; // time of reference
uint32_t ttl; // time to live (scadenza entry)
Expand Down Expand Up @@ -208,6 +187,8 @@ class U_EXPORT UCache {
{
U_TRACE(0, "UCache::entry(%u)", pos)

U_INTERNAL_DUMP("info->size = %u", info->size)

U_INTERNAL_ASSERT(pos <= (info->size - sizeof(uint32_t)))

cache_hash_table_entry* e = (cache_hash_table_entry*)(x + pos);
Expand All @@ -233,12 +214,13 @@ class U_EXPORT UCache {
U_INTERNAL_DUMP("*ptr = %u", value)
}

char* add(const char* key, uint32_t keylen, uint32_t datalen, uint32_t ttl);

void print(ostream& os, uint32_t& pos) const;

char* add(const char* key, uint32_t keylen, uint32_t datalen, uint32_t ttl);

private:
inline uint32_t hash(const char* key, uint32_t keylen) U_NO_EXPORT;
void init(UFile& _x, uint32_t size, bool bexist) U_NO_EXPORT;

UCache(const UCache&) {}
UCache& operator=(const UCache&) { return *this; }
Expand Down
46 changes: 44 additions & 2 deletions include/ulib/file.h
Expand Up @@ -137,7 +137,15 @@ class U_EXPORT UFile : public stat {
// PATH

void setRoot();
void setPath(const UString& path, const UString* environment = 0);
void setPath(const UString& path, const UString* environment = 0);
void setPath(const char* _pathname, const UString* environment = 0)
{
U_TRACE(0, "UFile::setPath(%S,%p)", _pathname, environment)

UString path(_pathname);

setPath(path, environment);
}

// NB: la stringa potrebbe non essere scrivibile e quindi path_relativ[path_relativ_len] potrebbe non essere '\0'...

Expand Down Expand Up @@ -260,6 +268,17 @@ class U_EXPORT UFile : public stat {
U_RETURN(result);
}

bool isPath()
{
U_TRACE(0, "UFile::isPath()")

U_CHECK_MEMORY

bool result = (pathname.empty() == false);

U_RETURN(result);
}

void close()
{
U_TRACE(0, "UFile::close()")
Expand Down Expand Up @@ -380,6 +399,28 @@ class U_EXPORT UFile : public stat {
U_RETURN(st_size);
}

static off_t getSize(int _fd)
{
U_TRACE(1, "UFile::getSize(%d)", _fd)

U_INTERNAL_ASSERT_DIFFERS(_fd, -1)

off_t result = U_SYSCALL(lseek, "%d,%u,%d", _fd, U_SEEK_BEGIN, SEEK_END);

U_RETURN(result);
}

static off_t getSize(const char* _pathname)
{
U_TRACE(1, "UFile::getSize(%S)", _pathname)

struct stat tmp;

if (U_SYSCALL(stat, "%S,%p", U_PATH_CONV(_pathname), &tmp) == 0) U_RETURN(tmp.st_size);

U_RETURN(0);
}

bool empty() const
{
U_TRACE(0, "UFile::empty()")
Expand Down Expand Up @@ -792,7 +833,8 @@ class U_EXPORT UFile : public stat {

static uint32_t buildFilenameListFrom(UVector<UString>& vec, const UString& arg, char sep = ',');

static uint32_t listContentOf(UVector<UString>& vec, const UString* dir = 0, const char* filter = 0, uint32_t filter_len = 0);
uint32_t listContentOf(UVector<UString>& vec, const char* filter, uint32_t filter_len);
static uint32_t listContentOf(UVector<UString>& vec, const UString* dir, const char* filter, uint32_t filter_len);
static void listRecursiveContentOf(UTree<UString>& tree, const UString* dir = 0, const char* filter = 0, uint32_t filter_len = 0);

// DEBUG
Expand Down
7 changes: 5 additions & 2 deletions include/ulib/file_config.h
Expand Up @@ -37,8 +37,6 @@ class U_EXPORT UFileConfig : public UFile {

// SERVICES

UString getData() const { return data; }

bool loadTable(UHashMap<UString>& table);
bool loadVector(UVector<UString>& vec, const char* name = 0);

Expand Down Expand Up @@ -113,6 +111,11 @@ class U_EXPORT UFileConfig : public UFile {

// EXT

bool processData();

UString getData() const { return data; }
void setData(const UString& _data) { data = _data; }

// This implementation of a Configuration reads properties
// from a legacy Windows initialization (.ini) file.
//
Expand Down
4 changes: 1 addition & 3 deletions include/ulib/net/server/client_image.h
Expand Up @@ -238,9 +238,7 @@ template <> class U_EXPORT UClientImage<USSLSocket> : public UClientImage_Base {

if (logbuf) UClientImage_Base::logCertificate(getSocket()->getPeerCertificate());

if (UClientImage_Base::newConnection()) U_RETURN(true);

U_RETURN(false);
return UClientImage_Base::newConnection();
}

// DEBUG
Expand Down
7 changes: 4 additions & 3 deletions include/ulib/net/server/plugin/mod_nocat.h
Expand Up @@ -68,11 +68,11 @@ class UModNoCatPeer : public UIPAddress, UEventTime {
#endif

protected:
time_t connected, expire, logout, ctime;
uint64_t traffic_done, traffic_available;
uint32_t ifindex, ctraffic, rulenum, index_AUTH;
UString ip, mac, token, user, ifname, label, gateway;
UCommand fw;
time_t connected, expire, logout, ctime;
uint32_t ctraffic, rulenum, index_AUTH, index_device, index_network;
int status;
bool allowed;

Expand Down Expand Up @@ -182,6 +182,7 @@ class U_EXPORT UNoCatPlugIn : public UServerPlugIn, UEventTime {
static UString* status_content;
static int fd_stderr, check_type;
static UVector<UIPAddress*>** vaddr;
static const UString* label_to_match;
static bool flag_check_peers_for_info;
static UHashMap<UModNoCatPeer*>* peers;
static time_t last_request, last_request_check, check_expire;
Expand All @@ -197,10 +198,10 @@ class U_EXPORT UNoCatPlugIn : public UServerPlugIn, UEventTime {

void setPeerListInfo();
void checkPeersForInfo();
void setStatusContent(UModNoCatPeer* peer);
bool checkAuthMessage(UModNoCatPeer* peer);
bool checkSignedData(const char* ptr, uint32_t len);
void addPeerInfo(UModNoCatPeer* peer, time_t logout);
void setStatusContent(UModNoCatPeer* peer, const UString& label);
void setRedirectLocation(UModNoCatPeer* peer, const UString& redirect, const Url& auth);
bool sendMsgToPortal(UCommand& cmd, uint32_t index_AUTH, const UString& msg, UString* poutput = 0);

Expand Down
18 changes: 2 additions & 16 deletions include/ulib/string.h
Expand Up @@ -273,22 +273,6 @@ class U_EXPORT UStringRep {

int compare(const UStringRep* rep) const { return compare(rep->str, rep->_length); }

int compare(const UStringRep* rep, uint32_t depth) const
{
U_TRACE(0, "UStringRep::compare(%.*S,%u)", U_min(_length, rep->_length) - depth, rep->str+depth, depth)

U_INTERNAL_ASSERT(depth <= _length)
U_INTERNAL_ASSERT(depth <= rep->_length)

int r = memcmp(str + depth, rep->str + depth, U_min(_length, rep->_length) - depth);

U_INTERNAL_DUMP("str[%u] = %.*S", depth, U_min(_length, rep->_length) - depth, str + depth)

if (r == 0) r = (_length - rep->_length);

U_RETURN(r);
}

int compare(uint32_t pos, uint32_t n1, const char* s, uint32_t n2) const
{
U_TRACE(0, "UStringRep::compare(%u,%u,%.*S,%u)", pos, n1, n2, s, n2)
Expand All @@ -302,6 +286,8 @@ class U_EXPORT UStringRep {
U_RETURN(r);
}

int compare(const UStringRep* rep, uint32_t depth) const __pure;

// Compare with ignore case

int comparenocase(const char* s, uint32_t n) const
Expand Down
4 changes: 2 additions & 2 deletions include/ulib/utility/uhttp.h
Expand Up @@ -516,8 +516,8 @@ class U_EXPORT UHTTP {
static UMimeMultipart* formMulti;
static UVector<UString>* form_name_value;

static void processHTTPForm();
static void resetForm(bool brmdir);
static uint32_t processHTTPForm();
static void resetForm(bool brmdir);

static void getFormValue(UString& value, const char* name, uint32_t len);
static void getFormValue(UString& value, uint32_t pos);
Expand Down
2 changes: 1 addition & 1 deletion src/ulib/base/base_trace.c
Expand Up @@ -314,7 +314,7 @@ static void handlerSignal(void)

char* env = getenv("UTRACE_LEVEL");

(void) u__snprintf(buffer, sizeof(buffer), "UTRACE=%s", env ? env : "0");
(void) u__snprintf(buffer, sizeof(buffer), "UTRACE=%s 10M 1", env ? env : "0");

(void) putenv(buffer);

Expand Down

0 comments on commit aaaaa6e

Please sign in to comment.