Permalink
Browse files

Add cgi support to json formatter.

  • Loading branch information...
kevingranade committed Oct 5, 2017
1 parent 56d90dc commit 3450f88554184755625f9958ddef6f17a4849a19
Showing with 183 additions and 16 deletions.
  1. +2 −0 LICENSE.txt
  2. +4 −4 Makefile
  3. +1 −1 tools/format/format.html
  4. +28 −11 tools/json_tools/format/format.cpp
  5. +148 −0 tools/json_tools/format/getpost.h
View
@@ -5,3 +5,5 @@ The Square font (data/font/square.ttf) is copyright Wouter van Oortmerssen, lice
GNU Unifont (data/font/unitfont.ttf) is licensed under GNU General Public License v2+ with the GNU font embedding exception. Visit http://unifoundry.com/LICENSE.txt for details.
CATCH unit-test framework is licensed under the Boost Software License. Visit https://github.com/philsquared/Catch/blob/master/LICENSE.txt to read the license.
getpost (tools/json_tools/format/getpost.h) is licensed under the MIT license, see file for text of license.
View
@@ -629,7 +629,7 @@ SOURCES = $(wildcard $(SRC_DIR)/*.cpp)
HEADERS = $(wildcard $(SRC_DIR)/*.h)
TESTSRC = $(wildcard tests/*.cpp)
TESTHDR = $(wildcard tests/*.h)
TOOLSRC = $(wildcard tools/json_tools/format/*.cpp)
TOOLSRC = $(wildcard tools/json_tools/format/*.[ch]*)
_OBJS = $(SOURCES:$(SRC_DIR)/%.cpp=%.o)
ifeq ($(TARGETSYSTEM),WINDOWS)
@@ -957,13 +957,13 @@ else
endif
style-json: new_json_whitelist json_formatter
xargs -a new_json_whitelist -L 1 ./json_formatter
xargs -a new_json_whitelist -L 1 format/json_formatter.cgi
style-all-json: json_formatter
find data -name "*.json" -print0 | xargs -0 -L 1 ./json_formatter
find data -name "*.json" -print0 | xargs -0 -L 1 format/json_formatter.cgi
json_formatter: tools/json_tools/format/format.cpp src/json.cpp
$(CXX) $(CXXFLAGS) -Itools/json_tools/format -Isrc tools/json_tools/format/format.cpp src/json.cpp -o json_formatter
$(CXX) $(CXXFLAGS) -Itools/json_tools/format -Isrc tools/json_tools/format/format.cpp src/json.cpp -o format/json_formatter.cgi
lint-check: json_whitelist $(ODIR)/lint.cache
View
@@ -22,7 +22,7 @@
}
var xhr = new XMLHttpRequest();
xhr.open('POST', '/tools/format/format.pl');
xhr.open('POST', '/format/json_formatter.cgi');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.timeout = 10000; // 10 seconds
@@ -1,7 +1,10 @@
#include "json.h"
#include "getpost.h"
#include <cstdlib>
#include <fstream>
#include <map>
#include <sstream>
#include <string>
@@ -73,20 +76,33 @@ int main( int argc, char *argv[] )
std::stringstream in;
std::stringstream out;
std::string filename;
std::string header;
// Expect a single filename for now.
if( argc == 2 ) {
filename = argv[1];
} else if( argc != 1 ) {
std::cout << "Supply a filename to style or no arguments." << std::endl;
exit( EXIT_FAILURE );
}
char *gateway_var = getenv( "GATEWAY_INTERFACE" );
if( gateway_var == nullptr ) {
// Expect a single filename for now.
if( argc == 2 ) {
filename = argv[1];
} else if( argc != 1 ) {
std::cout << "Supply a filename to style or no arguments." << std::endl;
exit( EXIT_FAILURE );
}
if( filename.empty() ) {
in << std::cin.rdbuf();
if( filename.empty() ) {
in << std::cin.rdbuf();
} else {
std::ifstream fin( filename, std::ios::binary );
in << fin.rdbuf();
}
} else {
std::ifstream fin( filename, std::ios::binary );
in << fin.rdbuf();
std::map<std::string, std::string> params;
initializePost( params );
std::string data = params[ "data" ];
if( data.empty() ) {
exit( -255 );
}
in.str( data );
header = "Content-type: application/json\n\n";
}
JsonOut jsout( out, true );
@@ -97,6 +113,7 @@ int main( int argc, char *argv[] )
out << std::endl;
if( filename.empty() ) {
std::cout << header;
std::cout << out.str() << std::endl;
} else {
if( in.str() == out.str() ) {
@@ -0,0 +1,148 @@
/*****************************************************************************
The MIT License
Copyright (c) 2007 Guy Rutenberg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*****************************************************************************/
#ifndef __GETPOST_H__
#define __GETPOST_H__
#include <cstring>
#include <string>
#include <stdlib.h>
#include <map>
#include <new>
std::string urlDecode( std::string str )
{
std::string temp;
int i;
char tmp[5], tmpchar;
strcpy( tmp, "0x" );
int size = str.size();
for( i = 0; i < size; i++ ) {
if( str[i] == '%' ) {
if( i + 2 < size ) {
tmp[2] = str[i + 1];
tmp[3] = str[i + 2];
tmp[4] = '\0';
tmpchar = ( char )strtol( tmp, NULL, 0 );
temp += tmpchar;
i += 2;
continue;
} else {
break;
}
} else if( str[i] == '+' ) {
temp += ' ';
} else {
temp += str[i];
}
}
return temp;
}
void initializeGet( std::map <std::string, std::string> &Get )
{
std::string tmpkey, tmpvalue;
std::string *tmpstr = &tmpkey;
char *raw_get = getenv( "QUERY_STRING" );
if( raw_get == NULL ) {
Get.clear();
return;
}
while( *raw_get != '\0' ) {
if( *raw_get == '&' ) {
if( tmpkey != "" ) {
Get[urlDecode( tmpkey )] = urlDecode( tmpvalue );
}
tmpkey.clear();
tmpvalue.clear();
tmpstr = &tmpkey;
} else if( *raw_get == '=' ) {
tmpstr = &tmpvalue;
} else {
( *tmpstr ) += ( *raw_get );
}
raw_get++;
}
//enter the last pair to the map
if( tmpkey != "" ) {
Get[urlDecode( tmpkey )] = urlDecode( tmpvalue );
tmpkey.clear();
tmpvalue.clear();
}
}
void initializePost( std::map <std::string, std::string> &Post )
{
std::string tmpkey, tmpvalue;
std::string *tmpstr = &tmpkey;
int content_length;
char *ibuffer;
char *buffer = NULL;
char *strlength = getenv( "CONTENT_LENGTH" );
if( strlength == NULL ) {
Post.clear();
return;
}
content_length = atoi( strlength );
if( content_length == 0 ) {
Post.clear();
return;
}
try {
buffer = new char[content_length * sizeof( char )];
} catch( std::bad_alloc xa ) {
Post.clear();
return;
}
if( fread( buffer, sizeof( char ), content_length, stdin ) != ( unsigned int )content_length ) {
Post.clear();
return;
}
*( buffer + content_length ) = '\0';
ibuffer = buffer;
while( *ibuffer != '\0' ) {
if( *ibuffer == '&' ) {
if( tmpkey != "" ) {
Post[urlDecode( tmpkey )] = urlDecode( tmpvalue );
}
tmpkey.clear();
tmpvalue.clear();
tmpstr = &tmpkey;
} else if( *ibuffer == '=' ) {
tmpstr = &tmpvalue;
} else {
( *tmpstr ) += ( *ibuffer );
}
ibuffer++;
}
//enter the last pair to the map
if( tmpkey != "" ) {
Post[urlDecode( tmpkey )] = urlDecode( tmpvalue );
tmpkey.clear();
tmpvalue.clear();
}
}
#endif /*__GETPOST_H__*/

0 comments on commit 3450f88

Please sign in to comment.