Skip to content
Snippets Groups Projects
Commit d4aae4b1 authored by Sergey Yakubov's avatar Sergey Yakubov
Browse files

fix

parent 86e33811
Branches
Tags
No related merge requests found
......@@ -3,7 +3,7 @@ package database
import "net/url"
func shouldEscape(c byte, db bool) bool {
if c == '$' || c == ' ' {
if c == '$' || c == ' ' || c == '%' {
return true
}
if !db {
......
package database
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestEncoding(t *testing.T) {
stream:=`ss$`
source :=`ads%&%41.sss`
streamEncoded := encodeStringForColName(stream)
sourceEncoded := encodeStringForDbName(source)
streamDecoded := decodeString(streamEncoded)
sourceDecoded := decodeString(sourceEncoded)
assert.Equal(t, streamDecoded, stream)
assert.Equal(t, sourceDecoded, source)
r := Request{
DbName: source,
DbCollectionName: stream,
GroupId: stream,
Op: "",
DatasetOp: false,
MinDatasetSize: 0,
ExtraParam: "",
}
err := encodeRequest(&r)
assert.Equal(t, r.DbCollectionName, streamEncoded)
assert.Equal(t, r.GroupId, streamEncoded)
assert.Equal(t, r.DbName, sourceEncoded)
assert.Nil(t,err)
}
......@@ -27,5 +27,5 @@ set(TEST_SOURCE_FILES ../../unittests/database/test_encoding.cpp)
set(TEST_LIBRARIES "${TARGET_NAME}")
include_directories(${ASAPO_CXX_COMMON_INCLUDE_DIR})
gtest(${TARGET_NAME} "${TEST_SOURCE_FILES}" "${TEST_LIBRARIES}")
gtest(${TARGET_NAME} "${TEST_SOURCE_FILES}" "${TEST_LIBRARIES}" "${CMAKE_CURRENT_SOURCE_DIR}/mongodb_client.cpp")
......@@ -5,7 +5,7 @@
namespace asapo {
bool ShouldEscape(char c, bool db) {
if (c == '$' || c == ' ') {
if (c == '$' || c == ' ' || c == '%') {
return true;
}
if (!db) {
......@@ -55,25 +55,22 @@ std::string Escape(const std::string &s, bool db) {
return t;
}
inline int ishex(int x)
{
return (x >= '0' && x <= '9') ||
(x >= 'a' && x <= 'f') ||
inline int ishex(int x) {
return (x >= '0' && x <= '9') ||
(x >= 'a' && x <= 'f') ||
(x >= 'A' && x <= 'F');
}
int decode(const char *s, char *dec)
{
char *o;
const char *end = s + strlen(s);
int decode(const char* s, char* dec) {
char* o;
const char* end = s + strlen(s);
int c;
for (o = dec; s <= end; o++) {
c = *s++;
// if (c == '+') c = ' ';
if (c == '%' && ( !ishex(*s++) ||
!ishex(*s++) ||
if (c == '%' && (!ishex(*s++) ||
!ishex(*s++) ||
!sscanf(s - 2, "%2x", &c)))
return -1;
if (dec) *o = c;
......@@ -82,7 +79,6 @@ int decode(const char *s, char *dec)
return o - dec;
}
std::string EncodeDbName(const std::string &dbname) {
return Escape(dbname, true);
}
......@@ -93,14 +89,14 @@ std::string EncodeColName(const std::string &colname) {
std::string DecodeName(const std::string &name) {
char decoded[name.size()];
auto res = decode(name.c_str(),decoded);
return res>=0?decoded:"";
auto res = decode(name.c_str(), decoded);
return res >= 0 ? decoded : "";
}
bool ShouldEscapeQuery(char c) {
char chars[] = "-[]{}()*+?\\.,^$|#";
for (auto i=0;i<strlen(chars);i++) {
if (c==chars[i]) {
for (auto i = 0; i < strlen(chars); i++) {
if (c == chars[i]) {
return true;
}
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment