|
|
@ -328,10 +328,10 @@ char *iguana_htmlresponse(char *retbuf,int32_t bufsize,int32_t *remainsp,int32_t |
|
|
|
n = iguana_htmlgen(&retbuf[n],bufsize-n,result,error,json,"iguana",Currentjsonstr); |
|
|
|
free_json(json); |
|
|
|
/*if ( n == 0 )
|
|
|
|
{ |
|
|
|
n = (int32_t)(strlen(html) + strlen(retstr) + 1); |
|
|
|
sprintf(retbuf+strlen(retbuf),html,retstr); |
|
|
|
}*/ |
|
|
|
{ |
|
|
|
n = (int32_t)(strlen(html) + strlen(retstr) + 1); |
|
|
|
sprintf(retbuf+strlen(retbuf),html,retstr); |
|
|
|
}*/ |
|
|
|
if ( freeflag != 0 ) |
|
|
|
free(retstr); |
|
|
|
if ( n > bufsize ) |
|
|
@ -342,327 +342,3 @@ char *iguana_htmlresponse(char *retbuf,int32_t bufsize,int32_t *remainsp,int32_t |
|
|
|
*remainsp = n; |
|
|
|
return(retbuf); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t Supernet_lineparse(char *key,int32_t keymax,char *value,int32_t valuemax,char *src) |
|
|
|
{ |
|
|
|
int32_t a,b,c,n = 0; |
|
|
|
key[0] = value[0] = 0; |
|
|
|
while ( (c= src[n]) == ' ' || c == '\t' || c == '\n' || c == '\t' ) |
|
|
|
n++; |
|
|
|
while ( (c= src[n]) != ':' && c != 0 ) |
|
|
|
{ |
|
|
|
*key++ = c; |
|
|
|
if ( ++n >= keymax-1 ) |
|
|
|
{ |
|
|
|
*key = 0; |
|
|
|
printf("lineparse overflow key.(%s)\n",src); |
|
|
|
return(-1); |
|
|
|
} |
|
|
|
} |
|
|
|
*key = 0; |
|
|
|
if ( src[n] != ':' ) |
|
|
|
return(n); |
|
|
|
n++; |
|
|
|
while ( (c= src[n]) == ' ' || c == '\t' ) |
|
|
|
n++; |
|
|
|
while ( (c= src[n]) != 0 && c != '\r' && c != '\n' ) |
|
|
|
{ |
|
|
|
if ( c == '%' && (a= src[n+1]) != 0 && (b= src[n+2]) != 0 ) |
|
|
|
c = ((unhex(a) << 4) | unhex(b)), n += 2; |
|
|
|
*value++ = c; |
|
|
|
n++; |
|
|
|
if ( n >= valuemax-1 ) |
|
|
|
{ |
|
|
|
*value = 0; |
|
|
|
printf("lineparse overflow.(%s)\n",src); |
|
|
|
return(-1); |
|
|
|
} |
|
|
|
} |
|
|
|
*value = 0; |
|
|
|
if ( src[n] != 0 ) |
|
|
|
{ |
|
|
|
n++; |
|
|
|
while ( (c= src[n]) == '\r' || c == '\n' ) |
|
|
|
n++; |
|
|
|
} |
|
|
|
printf("key.(%s) value.(%s)\n",key,value); |
|
|
|
return(n); |
|
|
|
} |
|
|
|
|
|
|
|
cJSON *SuperNET_urlconv(char *value,int32_t bufsize,char *urlstr) |
|
|
|
{ |
|
|
|
int32_t i,n,totallen,datalen,len = 0; cJSON *json,*array; char key[8192],*data; |
|
|
|
json = cJSON_CreateObject(); |
|
|
|
array = cJSON_CreateArray(); |
|
|
|
totallen = (int32_t)strlen(urlstr); |
|
|
|
while ( 1 ) |
|
|
|
{ |
|
|
|
for (i=len; urlstr[i]!=0; i++) |
|
|
|
if ( urlstr[i] == '\r' || urlstr[i] == '\n' ) |
|
|
|
break; |
|
|
|
if ( i == len && (urlstr[len] == '\r' || urlstr[len] == '\n') ) |
|
|
|
{ |
|
|
|
len++; |
|
|
|
continue; |
|
|
|
} |
|
|
|
urlstr[i] = 0; |
|
|
|
if ( (n= Supernet_lineparse(key,sizeof(key),value,bufsize,&urlstr[len])) > 0 ) |
|
|
|
{ |
|
|
|
if ( value[0] != 0 ) |
|
|
|
jaddstr(json,key,value); |
|
|
|
else jaddistr(array,key); |
|
|
|
len += (n + 1); |
|
|
|
if ( strcmp(key,"Content-Length") == 0 && (datalen= atoi(value)) > 0 ) |
|
|
|
{ |
|
|
|
data = &urlstr[totallen - datalen]; |
|
|
|
data[-1] = 0; |
|
|
|
//printf("post.(%s) (%c)\n",data,data[0]);
|
|
|
|
jaddstr(json,"POST",data); |
|
|
|
} |
|
|
|
} else break; |
|
|
|
} |
|
|
|
jadd(json,"lines",array); |
|
|
|
return(json); |
|
|
|
} |
|
|
|
|
|
|
|
char *SuperNET_rpcparse(struct supernet_info *myinfo,char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *postflagp,char *urlstr,char *remoteaddr) |
|
|
|
{ |
|
|
|
cJSON *tokens,*argjson,*json = 0; char symbol[16],buf[4096],urlmethod[16],*data,url[1024],*retstr,*token = 0; int32_t i,j,n; |
|
|
|
//printf("rpcparse.(%s)\n",urlstr);
|
|
|
|
for (i=0; i<sizeof(urlmethod)-1&&urlstr[i]!=0&&urlstr[i]!=' '; i++) |
|
|
|
urlmethod[i] = urlstr[i]; |
|
|
|
urlmethod[i++] = 0; |
|
|
|
n = i; |
|
|
|
//printf("URLMETHOD.(%s)\n",urlmethod);
|
|
|
|
*postflagp = (strcmp(urlmethod,"POST") == 0); |
|
|
|
for (i=0; i<sizeof(url)-1&&urlstr[n+i]!=0&&urlstr[n+i]!=' '; i++) |
|
|
|
url[i] = urlstr[n+i]; |
|
|
|
url[i++] = 0; |
|
|
|
n += i; |
|
|
|
j = i = 0; |
|
|
|
if ( strncmp(&url[i],"/api",strlen("/api")) == 0 ) |
|
|
|
{ |
|
|
|
*jsonflagp = 1; |
|
|
|
i += strlen("/api"); |
|
|
|
} else *jsonflagp = 0; |
|
|
|
if ( strncmp(&url[i],"/bitmap",strlen("/bitmap")) == 0 ) |
|
|
|
{ |
|
|
|
i += strlen("/bitmap"); |
|
|
|
*jsonflagp = 2; |
|
|
|
if ( url[i] == '/' ) |
|
|
|
i++; |
|
|
|
iguana_bitmap(retbuf,bufsize,&url[i]); |
|
|
|
return(retbuf); |
|
|
|
} |
|
|
|
//printf("URL.(%s)\n",url);
|
|
|
|
if ( strcmp(url,"/favicon.ico") == 0 ) |
|
|
|
{ |
|
|
|
*jsonflagp = -1; |
|
|
|
return(0); |
|
|
|
} |
|
|
|
if ( url[i] != '/' ) |
|
|
|
token = url; |
|
|
|
tokens = cJSON_CreateArray(); |
|
|
|
for (; url[i]!=0; i++) |
|
|
|
{ |
|
|
|
if ( url[i] == '/' ) |
|
|
|
{ |
|
|
|
url[i] = 0; |
|
|
|
if ( token != 0 ) |
|
|
|
jaddistr(tokens,token); |
|
|
|
token = &url[i+1]; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
if ( token != 0 ) |
|
|
|
jaddistr(tokens,token); |
|
|
|
if ( (json= SuperNET_urlconv(retbuf,bufsize,urlstr+n)) != 0 ) |
|
|
|
{ |
|
|
|
jadd(json,"tokens",tokens); |
|
|
|
jaddstr(json,"urlmethod",urlmethod); |
|
|
|
if ( (data= jstr(json,"POST")) == 0 || (argjson= cJSON_Parse(data)) == 0 ) |
|
|
|
{ |
|
|
|
argjson = cJSON_CreateObject(); |
|
|
|
if ( (n= cJSON_GetArraySize(tokens)) > 0 ) |
|
|
|
{ |
|
|
|
jaddstr(argjson,"agent",jstri(tokens,0)); |
|
|
|
if ( n > 1 ) |
|
|
|
{ |
|
|
|
if ( jstri(tokens,1) != 0 ) |
|
|
|
{ |
|
|
|
char *key,*value; |
|
|
|
strcpy(buf,jstri(tokens,1)); |
|
|
|
key = value = 0; |
|
|
|
i = 0; |
|
|
|
for (; buf[i]!=0; i++) |
|
|
|
{ |
|
|
|
if ( buf[i] == '?' ) |
|
|
|
{ |
|
|
|
buf[i] = 0; |
|
|
|
jaddstr(argjson,"method",buf); |
|
|
|
i++; |
|
|
|
key = &buf[i]; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
while ( buf[i] != 0 ) |
|
|
|
{ |
|
|
|
//printf("iter.[%s]\n",&buf[i]);
|
|
|
|
if ( buf[i] != 0 && key != 0 ) |
|
|
|
{ |
|
|
|
for (; buf[i]!=0; i++) |
|
|
|
{ |
|
|
|
if ( buf[i] == '=' ) |
|
|
|
{ |
|
|
|
buf[i] = 0; |
|
|
|
i++; |
|
|
|
//printf("got key.(%s)\n",key);
|
|
|
|
value = &buf[i]; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
if ( buf[i] != 0 && value != 0 ) |
|
|
|
{ |
|
|
|
for (; buf[i]!=0; i++) |
|
|
|
{ |
|
|
|
if ( buf[i] == '&' ) |
|
|
|
{ |
|
|
|
buf[i] = 0; |
|
|
|
jaddstr(argjson,key,value); |
|
|
|
i++; |
|
|
|
//printf("got value.(%s)\n",value);
|
|
|
|
value = 0; |
|
|
|
key = &buf[i]; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if ( key != 0 && value != 0 ) |
|
|
|
jaddstr(argjson,key,value); |
|
|
|
} else jaddstr(argjson,"method",buf); |
|
|
|
} |
|
|
|
for (i=2; i<n; i++) |
|
|
|
{ |
|
|
|
if ( i == n-1 ) |
|
|
|
jaddstr(argjson,"data",jstri(tokens,i)); |
|
|
|
else |
|
|
|
{ |
|
|
|
if ( strcmp(jstri(tokens,i),"coin") == 0 && strlen(jstri(tokens,i+1)) < 8 ) |
|
|
|
{ |
|
|
|
strcpy(symbol,jstri(tokens,i+1)); |
|
|
|
touppercase(symbol); |
|
|
|
jaddstr(argjson,jstri(tokens,i),symbol); |
|
|
|
} else jaddstr(argjson,jstri(tokens,i),jstri(tokens,i+1)); |
|
|
|
i++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if ( jstr(argjson,"method") == 0 ) |
|
|
|
{ |
|
|
|
free_json(argjson); |
|
|
|
return(0); |
|
|
|
} |
|
|
|
retstr = SuperNET_JSON(myinfo,argjson,remoteaddr); |
|
|
|
free_json(argjson); |
|
|
|
//printf("(%s) -> (%s) postflag.%d (%s)\n",urlstr,cJSON_Print(json),*postflagp,retstr);
|
|
|
|
return(retstr); |
|
|
|
} |
|
|
|
return(clonestr("{\"error\":\"couldnt process packet\"}")); |
|
|
|
} |
|
|
|
|
|
|
|
void iguana_rpcloop(void *args) |
|
|
|
{ |
|
|
|
struct supernet_info *myinfo = args; |
|
|
|
int32_t recvlen,bindsock,postflag,sock,remains,numsent,jsonflag,len; socklen_t clilen; |
|
|
|
char remoteaddr[64],jsonbuf[8192],*buf,*retstr,*space;//,*retbuf; ,n,i,m
|
|
|
|
struct sockaddr_in cli_addr; uint32_t ipbits,i,size = IGUANA_WIDTH*IGUANA_HEIGHT*16 + 512; uint16_t port; |
|
|
|
port = IGUANA_RPCPORT; |
|
|
|
bindsock = iguana_socket(1,"127.0.0.1",port); |
|
|
|
printf("iguana_rpcloop 127.0.0.1:%d bind sock.%d\n",port,bindsock); |
|
|
|
space = calloc(1,size); |
|
|
|
while ( bindsock >= 0 ) |
|
|
|
{ |
|
|
|
clilen = sizeof(cli_addr); |
|
|
|
//printf("ACCEPT (%s:%d) on sock.%d\n","127.0.0.1",port,bindsock);
|
|
|
|
sock = accept(bindsock,(struct sockaddr *)&cli_addr,&clilen); |
|
|
|
if ( sock < 0 ) |
|
|
|
{ |
|
|
|
//printf("iguana_rpcloop ERROR on accept usock.%d\n",sock);
|
|
|
|
continue; |
|
|
|
} |
|
|
|
memcpy(&ipbits,&cli_addr.sin_addr.s_addr,sizeof(ipbits)); |
|
|
|
expand_ipbits(remoteaddr,ipbits); |
|
|
|
//printf("RPC.%d for %x (%s)\n",sock,ipbits,ipaddr);
|
|
|
|
//printf("%p got.(%s) from %s | usock.%d ready.%u dead.%u\n",addr,H.command,addr->ipaddr,addr->usock,addr->ready,addr->dead);
|
|
|
|
memset(jsonbuf,0,sizeof(jsonbuf)); |
|
|
|
remains = (int32_t)(sizeof(jsonbuf) - 1); |
|
|
|
buf = jsonbuf; |
|
|
|
recvlen = 0; |
|
|
|
retstr = 0; |
|
|
|
while ( remains > 0 ) |
|
|
|
{ |
|
|
|
if ( (len= (int32_t)recv(sock,buf,remains,0)) < 0 ) |
|
|
|
{ |
|
|
|
if ( errno == EAGAIN ) |
|
|
|
{ |
|
|
|
printf("EAGAIN for len %d, remains.%d\n",len,remains); |
|
|
|
usleep(10000); |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
if ( len > 0 ) |
|
|
|
{ |
|
|
|
remains -= len; |
|
|
|
recvlen += len; |
|
|
|
buf = &buf[len]; |
|
|
|
retstr = SuperNET_rpcparse(myinfo,space,size,&jsonflag,&postflag,jsonbuf,remoteaddr); |
|
|
|
break; |
|
|
|
} else usleep(10000); |
|
|
|
//printf("got.(%s) %d remains.%d of total.%d\n",jsonbuf,recvlen,remains,len);
|
|
|
|
//retstr = iguana_rpcparse(space,size,&postflag,jsonbuf);
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
if ( retstr == 0 ) |
|
|
|
retstr = iguana_htmlresponse(space,size,&remains,1,retstr,retstr != space); |
|
|
|
if ( retstr != 0 ) |
|
|
|
{ |
|
|
|
i = 0; |
|
|
|
if ( 0 && postflag == 0 ) |
|
|
|
retstr = iguana_htmlresponse(space,size,&remains,1,retstr,retstr != space); |
|
|
|
else remains = (int32_t)strlen(retstr); |
|
|
|
//printf("POSTFLAG.%d\n",postflag);
|
|
|
|
//printf("RETBUF.(%s)\n",retstr);
|
|
|
|
while ( remains > 0 ) |
|
|
|
{ |
|
|
|
if ( (numsent= (int32_t)send(sock,&retstr[i],remains,MSG_NOSIGNAL)) < 0 ) |
|
|
|
{ |
|
|
|
if ( errno != EAGAIN && errno != EWOULDBLOCK ) |
|
|
|
{ |
|
|
|
//printf("%s: %s numsent.%d vs remains.%d len.%d errno.%d (%s) usock.%d\n",retstr,ipaddr,numsent,remains,recvlen,errno,strerror(errno),sock);
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
else if ( remains > 0 ) |
|
|
|
{ |
|
|
|
remains -= numsent; |
|
|
|
i += numsent; |
|
|
|
if ( remains > 0 ) |
|
|
|
printf("iguana sent.%d remains.%d of len.%d\n",numsent,remains,recvlen); |
|
|
|
} |
|
|
|
} |
|
|
|
if ( retstr != space) |
|
|
|
free(retstr); |
|
|
|
} |
|
|
|
if ( Currentjsonstr[0] != 0 ) |
|
|
|
strcpy(Prevjsonstr,Currentjsonstr); |
|
|
|
Currentjsonstr[0] = 0; |
|
|
|
//printf("done response sock.%d\n",sock);
|
|
|
|
closesocket(sock); |
|
|
|
} |
|
|
|
} |