Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions cmd/traffic_cache_tool/CacheDefs.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/** @file

Main program file for Cache Tool.

@section license License

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
*/

#include "CacheDefs.h"
#include <iostream>
using namespace std;
using namespace ts;

using ts::Errata;
namespace ts
{
std::ostream &
operator<<(std::ostream &s, Bytes const &n)
{
return s << n.count() << " bytes";
}
std::ostream &
operator<<(std::ostream &s, Kilobytes const &n)
{
return s << n.count() << " KB";
}
std::ostream &
operator<<(std::ostream &s, Megabytes const &n)
{
return s << n.count() << " MB";
}
std::ostream &
operator<<(std::ostream &s, Gigabytes const &n)
{
return s << n.count() << " GB";
}
std::ostream &
operator<<(std::ostream &s, Terabytes const &n)
{
return s << n.count() << " TB";
}

std::ostream &
operator<<(std::ostream &s, CacheStripeBlocks const &n)
{
return s << n.count() << " stripe blocks";
}
std::ostream &
operator<<(std::ostream &s, CacheStoreBlocks const &n)
{
return s << n.count() << " store blocks";
}
std::ostream &
operator<<(std::ostream &s, CacheDataBlocks const &n)
{
return s << n.count() << " data blocks";
}

Errata
URLparser::parseURL(StringView URI)
{
Errata zret;
static const StringView HTTP("http");
static const StringView HTTPS("https");
StringView scheme = URI.splitPrefix(':');
if ((strcasecmp(scheme, HTTP) == 0) || (strcasecmp(scheme, HTTPS) == 0)) {
StringView hostname = URI.splitPrefix(':');
if (!hostname) // i.e. port not present
{
}
}

return zret;
}

int
URLparser::getPort(std::string &fullURL, int &port_ptr, int &port_len)
{
url_matcher matcher;
int n_port = -1;
int u_pos = -1;

if (fullURL.find("https") == 0) {
u_pos = 8;
n_port = 443;
} else if (fullURL.find("http") == 0) {
u_pos = 7;
n_port = 80;
}
if (u_pos != -1) {
fullURL.insert(u_pos, ":@");
static const StringView HTTP("http");
static const StringView HTTPS("https");
StringView url(fullURL.data(), (int)fullURL.size());

url += 9;

StringView hostPort = url.splitPrefix(':');
if (hostPort) // i.e. port is present
{
StringView port = url.splitPrefix('/');
if (!port) // i.e. backslash is not present, then the rest of the url must be just port
port = url;
if (matcher.portmatch(port.begin(), port.size())) {
StringView text;
n_port = svtoi(port, &text);
if (text == port) {
port_ptr = fullURL.find(':', 9);
port_len = port.size();
return n_port;
}
}
}
return n_port;
} else {
std::cout << "No scheme provided for: " << fullURL << std::endl;
return -1;
}
}
}
156 changes: 112 additions & 44 deletions cmd/traffic_cache_tool/CacheDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
limitations under the License.
*/

#if !defined(CACHE_DEFS_H)
#ifndef CACHE_DEFS_H
#define CACHE_DEFS_H
#include <ts/I_Version.h>
#include <ts/Scalar.h>

#include <netinet/in.h>
#include <ts/Regex.h>
#include <ts/MemView.h>
#include "tsconfig/Errata.h"
#include <iostream>
namespace tag
{
struct bytes {
Expand All @@ -49,32 +53,6 @@ typedef Scalar<1024 * Kilobytes::SCALE, off_t, tag::bytes> Megabytes;
typedef Scalar<1024 * Megabytes::SCALE, off_t, tag::bytes> Gigabytes;
typedef Scalar<1024 * Gigabytes::SCALE, off_t, tag::bytes> Terabytes;

std::ostream &
operator<<(std::ostream &s, Bytes const &n)
{
return s << n.count() << " bytes";
}
std::ostream &
operator<<(std::ostream &s, Kilobytes const &n)
{
return s << n.count() << " KB";
}
std::ostream &
operator<<(std::ostream &s, Megabytes const &n)
{
return s << n.count() << " MB";
}
std::ostream &
operator<<(std::ostream &s, Gigabytes const &n)
{
return s << n.count() << " GB";
}
std::ostream &
operator<<(std::ostream &s, Terabytes const &n)
{
return s << n.count() << " TB";
}

// Units of allocation for stripes.
typedef Scalar<128 * Megabytes::SCALE, int64_t, tag::bytes> CacheStripeBlocks;
// Size measurement of cache storage.
Expand All @@ -83,22 +61,6 @@ typedef Scalar<8 * Kilobytes::SCALE, int64_t, tag::bytes> CacheStoreBlocks;
// Size unit for content stored in cache.
typedef Scalar<512, int64_t, tag::bytes> CacheDataBlocks;

std::ostream &
operator<<(std::ostream &s, CacheStripeBlocks const &n)
{
return s << n.count() << " stripe blocks";
}
std::ostream &
operator<<(std::ostream &s, CacheStoreBlocks const &n)
{
return s << n.count() << " store blocks";
}
std::ostream &
operator<<(std::ostream &s, CacheDataBlocks const &n)
{
return s << n.count() << " data blocks";
}

/** A cache span is a representation of raw storage.
It corresponds to a raw disk, disk partition, file, or directory.
*/
Expand Down Expand Up @@ -148,6 +110,7 @@ struct SpanHeader {

@internal nee VolHeadFooter
*/
// the counterpart of this structure in ATS is called VolHeaderFooter
class StripeMeta
{
public:
Expand All @@ -167,10 +130,18 @@ class StripeMeta
uint32_t dirty;
uint32_t sector_size;
uint32_t unused; // pad out to 8 byte boundary
uint16_t freelist[1];
};

/*
@internal struct Dir in P_CacheDir.h
* size: 10bytes
*/

class CacheDirEntry
{
public:
#if 0
unsigned int offset : 24;
unsigned int big : 2;
unsigned int size : 6;
Expand All @@ -181,11 +152,108 @@ class CacheDirEntry
unsigned int token : 1;
unsigned int next : 16;
uint16_t offset_high;
#else
uint16_t w[5];
#endif
};

class CacheVolume
{
};

class URLparser
{
public:
bool verifyURL(std::string &url1);
Errata parseURL(StringView URI);
int getPort(std::string &fullURL, int &port_ptr, int &port_len);

private:
// DFA regex;
};

class CacheURL
{
public:
in_port_t port;
std::string scheme;
std::string url;
std::string hostname;
std::string path;
std::string query;
std::string params;
std::string fragments;
std::string user;
std::string password;
CacheURL(int port_, ts::StringView b_hostname, ts::StringView b_path, ts::StringView b_params, ts::StringView b_query,
ts::StringView b_fragments)
{
hostname.assign(b_hostname.begin(), b_hostname.size());
port = port_;
path.assign(b_path.begin(), b_path.size());
params.assign(b_params.begin(), b_params.size());
query.assign(b_query.begin(), b_query.size());
fragments.assign(b_fragments.begin(), b_fragments.size());
}

CacheURL(ts::StringView blob, int port_)
{
url.assign(blob.begin(), blob.size());
port = port_;
}

void
setCredential(char *p_user, int user_len, char *p_pass, int pass_len)
{
user.assign(p_user, user_len);
password.assign(p_pass, pass_len);
}
};
}

class DFA;
// this class matches url of the format : scheme://hostname:port/path;params?query
struct url_matcher {
// R"(^https?\:\/\/^[a-z A-Z 0-9]\.[a-z A-Z 0-9 \.]+)"
url_matcher()
{
/*if (regex.compile(R"(^https?\:\/\/^[a-z A-Z 0-9][\. a-z A-Z 0-9 ]+(\:[0-9]\/)?.*))") != 0) {
std::cout<<"Check your regular expression"<<std::endl;
}*/
// (\w+\:[\w\W]+\@)? (:[0-9]+)?(\/.*)
if (regex.compile(R"(^(https?\:\/\/)") != 0) {
std::cout << "Check your regular expression" << std::endl;
return;
}
if (port.compile(R"([0-9]+$)") != 0) {
std::cout << "Check your regular expression" << std::endl;
return;
}
}

~url_matcher() {}
uint8_t
match(const char *hostname) const
{
if (regex.match(hostname) != -1)
return 1;
// if(url_with_user.match(hostname) != -1)
// return 2;
return 0;
}
uint8_t
portmatch(const char *hostname, int length) const
{
if (port.match(hostname, length) != -1)
return 1;
// if(url_with_user.match(hostname) != -1)
// return 2;
return 0;
}

private:
DFA port;
DFA regex;
};

#endif // CACHE_DEFS_H
Loading