aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Pouar <thepouar@gmail.com>2017-02-18 17:00:55 -0600
committerGravatar Pouar <thepouar@gmail.com>2017-02-18 17:00:55 -0600
commit95231c5e3765a287055094adf5ac1db62feca0b3 (patch)
tree951f78eda2eac2a686a798e8b04ecfb4930bd8c5
parentforgot a ) (diff)
better error checking
use spawn-fcgi to utilize more threads
-rw-r--r--.gitmodules3
-rw-r--r--Makefile4
-rw-r--r--identifur.c17
-rw-r--r--identifurfcgi.c98
m---------qs_parse0
5 files changed, 57 insertions, 65 deletions
diff --git a/.gitmodules b/.gitmodules
index 8364bdf..de6779c 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -4,3 +4,6 @@
[submodule "php-xxhash"]
path = php-xxhash
url = https://github.com/Megasaxon/php-xxhash.git
+[submodule "qs_parse"]
+ path = qs_parse
+ url = https://github.com/bartgrantham/qs_parse.git
diff --git a/Makefile b/Makefile
index d95a4fb..2d97aff 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
default:
- gcc -O2 -o identifur xxHash/xxhash.c identifur.c -s
+ gcc -O2 -o identifur xxHash/xxhash.c identifur.c -lbsd
fcgi:
- gcc -O2 -o identifurfcgi xxHash/xxhash.c identifurfcgi.c -lfcgi -s
+ gcc -O2 -o identifurfcgi qs_parse/qs_parse.c xxHash/xxhash.c identifurfcgi.c -lfcgi -lbsd -s
clean:
rm -f identifurfcgi identifur
all: default fcgi
diff --git a/identifur.c b/identifur.c
index 45b131b..57ae328 100644
--- a/identifur.c
+++ b/identifur.c
@@ -2,6 +2,8 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
+#include <float.h>
+#include <bsd/bsd.h>
#include "xxHash/xxhash.h"
/*
* You need xxhash.c and xxhash.h from https://github.com/Cyan4973/xxHash to compile this
@@ -9,6 +11,7 @@
*/
int main(int argc, char **argv)
{
+ const char *errstr;
if(argc<2)
{
fprintf(stderr,"syntax: identifur STRING [HEIGHT]\n");
@@ -17,13 +20,19 @@ int main(int argc, char **argv)
uint64_t hash = XXH32(argv[1], strlen(argv[1]), 0);
double height=1024.0;
double newheight;
- if( argc > 2)
+ if( argc < 3)
{
- newheight=atoi(argv[2]);
+ newheight=1024.0;
}
else
- {
- newheight=1024.0;
+ {
+ newheight=(double)strtonum(argv[2],0,(int)DBL_MAX,&errstr);
+ if(errstr!=NULL)
+ {
+ fprintf(stderr,"variable 'height' is %s\n",errstr);
+ exit(1);
+
+ }
}
uint16_t rgb1=((hash >> 20) & 0xfff);
uint16_t rgb2=((hash >> 8) & 0xfff);
diff --git a/identifurfcgi.c b/identifurfcgi.c
index 2423982..61f9689 100644
--- a/identifurfcgi.c
+++ b/identifurfcgi.c
@@ -1,10 +1,13 @@
+#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
+#include <float.h>
+#include <bsd/bsd.h>
#include "xxHash/xxhash.h"
-
-#include <fcgiapp.h>
+#include "qs_parse/qs_parse.h"
+#include <fcgi_stdio.h>
/*
* You need xxhash.c and xxhash.h from https://github.com/Cyan4973/xxHash to compile this
* gcc -O2 -o identifur xxhash.c identifur.c -s
@@ -12,11 +15,12 @@
// Helper macro to convert two-character hex strings to character value
#define ToHex(Y) (Y>='0'&&Y<='9'?Y-'0':Y-'A'+10)
-char InputData[4096];
-
+char *InputData;
+char *kvpairs[256];
+char value[256];
-char getValue[100] = "";
-char getHeight[100] = "";
+char getValue[256];
+char getHeight[256];
double height=1024.0;
double newheight;
uint64_t hash;
@@ -26,68 +30,45 @@ uint8_t tail;
uint8_t ears;
uint8_t wings;
uint8_t clothes;
-uint8_t basepath[4096];
-uint8_t tailpaths[4][4096];
-uint8_t earspaths[4][4096];
-uint8_t wingspaths[4][4096];
-uint8_t clothespaths[4][4096];
-char *querystring;
-void getParam(const char *Name, char *Value) {
- char *pos1 = strstr(InputData, Name);
-
- if (pos1) {
- pos1 += strlen(Name);
-
- if (*pos1 == '=') {
- pos1++;
-
- while (*pos1 && *pos1 != '&') {
- if (*pos1 == '%') {
- *Value++ = (char)ToHex(pos1[1]) * 16 + ToHex(pos1[2]);
- pos1 += 3;
- } else if( *pos1=='+' ) {
- *Value++ = ' ';
- pos1++;
- } else {
- *Value++ = *pos1++;
- }
- }
-
- *Value++ = '\0';
- return;
- }
-
- }
-
- strcpy(Value, "");
- return;
-}
+char basepath[4096];
+char tailpaths[4][4096];
+char earspaths[4][4096];
+char wingspaths[4][4096];
+char clothespaths[4][4096];
int main(int argc, char **argv)
{
- int sock;
- FCGX_Request request;
- FCGX_Init();
- sock = FCGX_OpenSocket("/tmp/identifurfcgi.sock", 100);
- FCGX_InitRequest(&request, sock, 0);
- while (FCGX_Accept_r(&request) >= 0)
+ const char *errstr;
+ while (FCGI_Accept() >= 0)
{
- FCGX_FPrintF(request.out,"Content-Type: image/svg+xml\r\n\r\n");
- querystring=FCGX_GetParam("QUERY_STRING", request.envp);
- strcpy(InputData, querystring);
- getParam("value", getValue);
- getParam("height", getHeight);
+ InputData = strdup(getenv("QUERY_STRING"));
+ qs_parse(InputData, kvpairs, 256);
+ if (qs_scanvalue("value", InputData, getValue, sizeof(getValue)) == NULL )
+ {
+ printf("Content-Type: text/plain\r\n\r\n");
+ printf("GET variable 'value' is undefined");
+ continue;
+ }
+
hash = XXH32(getValue, strlen(getValue), 0);
- if(strlen(getHeight)>0)
+ if(qs_scanvalue("height", InputData, getHeight, sizeof(getHeight)) == NULL)
{
- newheight=atoi(getHeight);
+ newheight=1024.0;
}
else
- {
- newheight=1024.0;
+ {
+ newheight=(double)strtonum(getHeight,0,(int)DBL_MAX,&errstr);
+ if(errstr!=NULL)
+ {
+ printf("Content-Type: text/plain\r\n\r\n");
+ printf("GET variable 'height' is %s",errstr);
+ continue;
+
+ }
}
+ printf("Content-Type: image/svg+xml\r\n\r\n");
rgb1=((hash >> 20) & 0xfff);
rgb2=((hash >> 8) & 0xfff);
tail=((hash >> 6) & 0b11);
@@ -111,8 +92,7 @@ int main(int argc, char **argv)
sprintf(clothespaths[1],"<path d=\"m471.86523 391.59375c-36.84971 19.21281-64.12163 79.24861-74.38671 105.52344 14.75047 5.88404 40.51051 16.70007 55.05273 18.63281 6.40702-29.84712 19.32613-44.06446 20.72656-48.61523.13344 54.82647-.3092 111.71392-1.47656 169.51367h104.7207c-1.28301-65.59488-2.1067-127.58323-1.58593-172.37696 4.29705 13.46937 9.96287 31.74774 12.73046 41.25586 14.16889 0 36.5531-8.54644 49.97461-10.67382-8.75523-31.37734-25.56647-68.64499-61.62109-102.17579-31.3279 19.14482-71.06851 29.99383-104.13477-1.08398z\" fill=\"#%x\" fill-rule=\"evenodd\" stroke=\"#%x\" transform=\"scale(.26458333)\"/>",rgb2,rgb2);
sprintf(clothespaths[2],"<path d=\"m159.96584 210.77731-7.43303-42.33074c-.33947-17.35532-.5574-33.7564-.41961-45.60808 1.13692 3.56377 2.636 8.39993 3.36826 10.91562 3.74885 0 9.67134-2.26125 13.22245-2.82412-2.31649-8.30192-6.76446-18.16232-16.30391-27.03401-8.28884 5.0654-18.80354 7.93587-27.55233-.2868-9.74982 5.08339-16.96551 20.96786-19.68148 27.91974 3.90273 1.55682 10.71841 4.41856 14.56604 4.92993 1.69519-7.89705 5.11337-11.65872 5.4839-12.86278.0353 14.50617-.0818 29.55764-.39068 44.8505l-10.48659 41.95676z\" fill=\"#%x\" fill-rule=\"evenodd\" stroke=\"#%x\" stroke-width=\".26458332\"/>",rgb2,rgb2);
sprintf(clothespaths[3],"<ellipse cx=\"138.94554\" cy=\"142.06779\" rx=\"9.4711866\" ry=\"35.588699\" style=\"fill:#%x;fill-rule:evenodd;stroke:#%x;stroke-width:.26458332;stroke-linecap:round;stroke-linejoin:round\"/>",rgb2,rgb2);
- FCGX_FPrintF(request.out,"<svg height=\"%u\" viewBox=\"0 0 %f %f\" width=\"%u\" xmlns=\"http://www.w3.org/2000/svg\"><g transform=\"scale(%f)\">%s\n%s%s%s%s</g>\n</svg>\n",(int)newheight,(newheight/height)*270.93332,(newheight/height)*270.93333,(int)newheight,newheight/height,basepath,tailpaths[tail],earspaths[ears],wingspaths[wings],clothespaths[clothes]);
- FCGX_Finish_r(&request);
+ printf("<svg height=\"%u\" viewBox=\"0 0 %f %f\" width=\"%u\" xmlns=\"http://www.w3.org/2000/svg\"><g transform=\"scale(%f)\">%s\n%s%s%s%s</g>\n</svg>\n",(int)newheight,(newheight/height)*270.93332,(newheight/height)*270.93333,(int)newheight,newheight/height,basepath,tailpaths[tail],earspaths[ears],wingspaths[wings],clothespaths[clothes]);
}
return EXIT_SUCCESS;
}
diff --git a/qs_parse b/qs_parse
new file mode 160000
+Subproject 5f1a1933a8ae3877d299b4bd758856f870f4923