xref: /petsc/src/sys/webclient/client.c (revision f253e43cc674c8507d337bbb5ef3ec57f9e3fddc)
1b967cddfSBarry Smith 
20efc6a03SBarry Smith #include <petscwebclient.h>
3bb04b57dSBarry Smith #pragma clang diagnostic ignored "-Wdeprecated-declarations"
445e40e47SBarry Smith #pragma gcc diagnostic ignored "-Wdeprecated-declarations"
5b967cddfSBarry Smith 
6b967cddfSBarry Smith static BIO *bio_err = NULL;
7b967cddfSBarry Smith 
8b967cddfSBarry Smith #define PASSWORD "password"
9b967cddfSBarry Smith 
104a285bdaSBarry Smith #if defined(PETSC_USE_SSL_CERTIFICATE)
11b967cddfSBarry Smith static int password_cb(char *buf,int num, int rwflag,void *userdata)
12b967cddfSBarry Smith {
13b967cddfSBarry Smith   if (num < strlen(PASSWORD)+1) return(0);
14b967cddfSBarry Smith   strcpy(buf,PASSWORD);
15b967cddfSBarry Smith   return(strlen(PASSWORD));
16b967cddfSBarry Smith }
17b967cddfSBarry Smith #endif
18b967cddfSBarry Smith 
19b967cddfSBarry Smith static void sigpipe_handle(int x)
20b967cddfSBarry Smith {
21b967cddfSBarry Smith }
22b967cddfSBarry Smith 
234683183fSBarry Smith /*@C
24b967cddfSBarry Smith     PetscSSLInitializeContext - Set up an SSL context suitable for initiating HTTPS requests.
25b967cddfSBarry Smith 
264683183fSBarry Smith     Output Parameter:
274683183fSBarry Smith .   octx - the SSL_CTX to be passed to PetscHTTPSConnect
28b967cddfSBarry Smith 
294683183fSBarry Smith     Level: advanced
304683183fSBarry Smith 
314683183fSBarry Smith     If PETSc was ./configure -with-ssl-certificate requires the user have created a self-signed certificate with
3268e69593SBarry Smith $    saws/CA.pl  -newcert  (using the passphrase of password)
33b967cddfSBarry Smith $    cat newkey.pem newcert.pem > sslclient.pem
34b967cddfSBarry Smith 
35b967cddfSBarry Smith     and put the resulting file in either the current directory (with the application) or in the home directory. This seems kind of
36b967cddfSBarry Smith     silly but it was all I could figure out.
37b967cddfSBarry Smith 
384683183fSBarry Smith .seealso: PetscSSLDestroyContext(), PetscHTTPSConnect(), PetscHTTPSRequest()
394683183fSBarry Smith 
404683183fSBarry Smith @*/
41b967cddfSBarry Smith PetscErrorCode PetscSSLInitializeContext(SSL_CTX **octx)
42b967cddfSBarry Smith {
43b967cddfSBarry Smith     SSL_CTX        *ctx;
444a285bdaSBarry Smith #if defined(PETSC_USE_SSL_CERTIFICATE)
45b967cddfSBarry Smith     char           keyfile[PETSC_MAX_PATH_LEN];
46b967cddfSBarry Smith     PetscBool      exists;
47b967cddfSBarry Smith     PetscErrorCode ierr;
48b967cddfSBarry Smith #endif
49b967cddfSBarry Smith 
50b967cddfSBarry Smith     PetscFunctionBegin;
51b967cddfSBarry Smith     if (!bio_err){
52b967cddfSBarry Smith       SSL_library_init();
53b967cddfSBarry Smith       SSL_load_error_strings();
54b967cddfSBarry Smith       bio_err = BIO_new_fp(stderr,BIO_NOCLOSE);
55b967cddfSBarry Smith     }
56b967cddfSBarry Smith 
57b967cddfSBarry Smith     /* Set up a SIGPIPE handler */
58b967cddfSBarry Smith     signal(SIGPIPE,sigpipe_handle);
59b967cddfSBarry Smith 
60ecd1d7b8SBarry Smith /* suggested at https://mta.openssl.org/pipermail/openssl-dev/2015-May/001449.html */
61ecd1d7b8SBarry Smith #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
62ecd1d7b8SBarry Smith     ctx  = SSL_CTX_new(TLS_client_method());
63ecd1d7b8SBarry Smith #else
64ecd1d7b8SBarry Smith     ctx  = SSL_CTX_new(SSLv23_client_method());
65ecd1d7b8SBarry Smith #endif
665dc0f0a4SBarry Smith     SSL_CTX_set_mode(ctx,SSL_MODE_AUTO_RETRY);
67b967cddfSBarry Smith 
684a285bdaSBarry Smith #if defined(PETSC_USE_SSL_CERTIFICATE)
69b967cddfSBarry Smith     /* Locate keyfile */
70b967cddfSBarry Smith     ierr = PetscStrcpy(keyfile,"sslclient.pem");CHKERRQ(ierr);
71b967cddfSBarry Smith     ierr = PetscTestFile(keyfile,'r',&exists);CHKERRQ(ierr);
72b967cddfSBarry Smith     if (!exists) {
73b967cddfSBarry Smith       ierr = PetscGetHomeDirectory(keyfile,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
74b967cddfSBarry Smith       ierr = PetscStrcat(keyfile,"/");CHKERRQ(ierr);
75b967cddfSBarry Smith       ierr = PetscStrcat(keyfile,"sslclient.pem");CHKERRQ(ierr);
76b967cddfSBarry Smith       ierr = PetscTestFile(keyfile,'r',&exists);CHKERRQ(ierr);
77b967cddfSBarry Smith       if (!exists) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate sslclient.pem file in current directory or home directory");
78b967cddfSBarry Smith     }
79b967cddfSBarry Smith 
80b967cddfSBarry Smith     /* Load our keys and certificates*/
81b967cddfSBarry Smith     if (!(SSL_CTX_use_certificate_chain_file(ctx,keyfile))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot read certificate file");
82b967cddfSBarry Smith 
83b967cddfSBarry Smith     SSL_CTX_set_default_passwd_cb(ctx,password_cb);
84b967cddfSBarry Smith     if (!(SSL_CTX_use_PrivateKey_file(ctx,keyfile,SSL_FILETYPE_PEM))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot read key file");
85b967cddfSBarry Smith #endif
86b967cddfSBarry Smith 
87b967cddfSBarry Smith     *octx = ctx;
88b967cddfSBarry Smith     PetscFunctionReturn(0);
89b967cddfSBarry Smith }
90b967cddfSBarry Smith 
914683183fSBarry Smith /*@C
924683183fSBarry Smith      PetscSSLDestroyContext - frees a SSL_CTX obtained with PetscSSLInitializeContext()
934683183fSBarry Smith 
944683183fSBarry Smith      Input Parameter:
954683183fSBarry Smith .     ctx - the SSL_CTX
964683183fSBarry Smith 
974683183fSBarry Smith     Level: advanced
984683183fSBarry Smith 
994683183fSBarry Smith .seealso: PetscSSLInitializeContext(), PetscHTTPSConnect()
1004683183fSBarry Smith @*/
101b967cddfSBarry Smith PetscErrorCode PetscSSLDestroyContext(SSL_CTX *ctx)
102b967cddfSBarry Smith {
103b967cddfSBarry Smith   PetscFunctionBegin;
104b967cddfSBarry Smith   SSL_CTX_free(ctx);
105b967cddfSBarry Smith   PetscFunctionReturn(0);
106b967cddfSBarry Smith }
107b967cddfSBarry Smith 
1084683183fSBarry Smith static PetscErrorCode PetscHTTPBuildRequest(const char type[],const char url[],const char header[],const char ctype[],const char body[],char **outrequest)
109b967cddfSBarry Smith {
110b967cddfSBarry Smith   char           *request=0;
11193e1d32fSBarry Smith   char           contentlength[40],contenttype[80],*path,*host;
1127a3410edSBarry Smith   size_t         request_len,headlen,bodylen,contentlen,pathlen,hostlen,typelen,contenttypelen = 0;
113b967cddfSBarry Smith   PetscErrorCode ierr;
114b967cddfSBarry Smith   PetscBool      flg;
115b967cddfSBarry Smith 
116b967cddfSBarry Smith   PetscFunctionBegin;
11793e1d32fSBarry Smith   ierr = PetscStrallocpy(url,&host);CHKERRQ(ierr);
11893e1d32fSBarry Smith   ierr = PetscStrchr(host,'/',&path);CHKERRQ(ierr);
11993e1d32fSBarry Smith   if (!path) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"url must contain / it is %s",url);
120c245270aSBarry Smith   *path = 0;
12193e1d32fSBarry Smith   ierr  = PetscStrlen(host,&hostlen);CHKERRQ(ierr);
12293e1d32fSBarry Smith 
12393e1d32fSBarry Smith   ierr = PetscStrchr(url,'/',&path);CHKERRQ(ierr);
12493e1d32fSBarry Smith   ierr = PetscStrlen(path,&pathlen);CHKERRQ(ierr);
12593e1d32fSBarry Smith 
126b967cddfSBarry Smith   if (header) {
127b967cddfSBarry Smith     ierr = PetscStrendswith(header,"\r\n",&flg);CHKERRQ(ierr);
128b967cddfSBarry Smith     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"header must end with \\r\\n");
129b967cddfSBarry Smith   }
130b967cddfSBarry Smith 
131b967cddfSBarry Smith   ierr = PetscStrlen(type,&typelen);CHKERRQ(ierr);
132b967cddfSBarry Smith   if (ctype) {
133b967cddfSBarry Smith     ierr = PetscSNPrintf(contenttype,80,"Content-Type: %s\r\n",ctype);CHKERRQ(ierr);
134b967cddfSBarry Smith     ierr = PetscStrlen(contenttype,&contenttypelen);CHKERRQ(ierr);
135b967cddfSBarry Smith   }
136b967cddfSBarry Smith   ierr = PetscStrlen(header,&headlen);CHKERRQ(ierr);
137b967cddfSBarry Smith   ierr = PetscStrlen(body,&bodylen);CHKERRQ(ierr);
138b967cddfSBarry Smith   ierr = PetscSNPrintf(contentlength,40,"Content-Length: %d\r\n\r\n",(int)bodylen);CHKERRQ(ierr);
139b967cddfSBarry Smith   ierr = PetscStrlen(contentlength,&contentlen);CHKERRQ(ierr);
140b967cddfSBarry Smith 
141b967cddfSBarry Smith   /* Now construct our HTTP request */
14293e1d32fSBarry Smith   request_len = typelen + 1 + pathlen + hostlen + 100 + headlen + contenttypelen + contentlen + bodylen + 1;
143fe278a28SBarry Smith   ierr = PetscMalloc1(request_len,&request);CHKERRQ(ierr);
144b967cddfSBarry Smith   ierr = PetscStrcpy(request,type);CHKERRQ(ierr);
145b967cddfSBarry Smith   ierr = PetscStrcat(request," ");CHKERRQ(ierr);
14693e1d32fSBarry Smith   ierr = PetscStrcat(request,path);CHKERRQ(ierr);
14793e1d32fSBarry Smith   ierr = PetscStrcat(request," HTTP/1.1\r\nHost: ");CHKERRQ(ierr);
14893e1d32fSBarry Smith   ierr = PetscStrcat(request,host);CHKERRQ(ierr);
14993e1d32fSBarry Smith   ierr = PetscFree(host);CHKERRQ(ierr);
15093e1d32fSBarry Smith   ierr = PetscStrcat(request,"\r\nUser-Agent:PETScClient\r\n");CHKERRQ(ierr);
151b967cddfSBarry Smith   ierr = PetscStrcat(request,header);CHKERRQ(ierr);
152b967cddfSBarry Smith   if (ctype) {
153b967cddfSBarry Smith     ierr = PetscStrcat(request,contenttype);CHKERRQ(ierr);
154b967cddfSBarry Smith   }
155b967cddfSBarry Smith   ierr = PetscStrcat(request,contentlength);CHKERRQ(ierr);
156b967cddfSBarry Smith   ierr = PetscStrcat(request,body);CHKERRQ(ierr);
157b967cddfSBarry Smith   ierr = PetscStrlen(request,&request_len);CHKERRQ(ierr);
158b967cddfSBarry Smith   ierr = PetscInfo1(NULL,"HTTPS request follows: \n%s\n",request);CHKERRQ(ierr);
159b967cddfSBarry Smith 
16004102261SBarry Smith   *outrequest = request;
16104102261SBarry Smith   PetscFunctionReturn(0);
16204102261SBarry Smith }
16304102261SBarry Smith 
16404102261SBarry Smith 
1654683183fSBarry Smith /*@C
16604102261SBarry Smith      PetscHTTPSRequest - Send a request to an HTTPS server
16704102261SBarry Smith 
16804102261SBarry Smith    Input Parameters:
16904102261SBarry Smith +   type - either "POST" or "GET"
17004102261SBarry Smith .   url -  URL of request host/path
17104102261SBarry Smith .   header - additional header information, may be NULL
17204102261SBarry Smith .   ctype - data type of body, for example application/json
17304102261SBarry Smith .   body - data to send to server
17404102261SBarry Smith .   ssl - obtained with PetscHTTPSConnect()
17504102261SBarry Smith -   buffsize - size of buffer
17604102261SBarry Smith 
17704102261SBarry Smith    Output Parameter:
17804102261SBarry Smith .   buff - everything returned from server
1794683183fSBarry Smith 
1804683183fSBarry Smith     Level: advanced
1814683183fSBarry Smith 
1824683183fSBarry Smith .seealso: PetscHTTPRequest(), PetscHTTPSConnect(), PetscSSLInitializeContext(), PetscSSLDestroyContext(), PetscPullJSONValue()
1834683183fSBarry Smith 
1844683183fSBarry Smith @*/
18504102261SBarry Smith PetscErrorCode PetscHTTPSRequest(const char type[],const char url[],const char header[],const char ctype[],const char body[],SSL *ssl,char buff[],size_t buffsize)
18604102261SBarry Smith {
18704102261SBarry Smith   char           *request;
18804102261SBarry Smith   int            r;
18904102261SBarry Smith   size_t         request_len,len;
19004102261SBarry Smith   PetscErrorCode ierr;
1915dc0f0a4SBarry Smith   PetscBool      foundbody = PETSC_FALSE;
19204102261SBarry Smith 
19304102261SBarry Smith   PetscFunctionBegin;
19404102261SBarry Smith   ierr = PetscHTTPBuildRequest(type,url,header,ctype,body,&request);CHKERRQ(ierr);
19504102261SBarry Smith   ierr = PetscStrlen(request,&request_len);CHKERRQ(ierr);
19604102261SBarry Smith 
197d8dcb26dSBarry Smith   r = SSL_write(ssl,request,(int)request_len);
198b967cddfSBarry Smith   switch (SSL_get_error(ssl,r)){
199b967cddfSBarry Smith     case SSL_ERROR_NONE:
200d8dcb26dSBarry Smith       if (request_len != (size_t)r) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Incomplete write to SSL socket");
201b967cddfSBarry Smith       break;
202b967cddfSBarry Smith     default:
203b967cddfSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"SSL socket write problem");
204b967cddfSBarry Smith   }
205b967cddfSBarry Smith 
2065dc0f0a4SBarry Smith   /* Now read the server's response, globus sends it in two chunks hence must read a second time if needed */
207580bdb30SBarry Smith   ierr      = PetscArrayzero(buff,buffsize);CHKERRQ(ierr);
2085dc0f0a4SBarry Smith   len       = 0;
2095dc0f0a4SBarry Smith   foundbody = PETSC_FALSE;
2105dc0f0a4SBarry Smith   do {
2115dc0f0a4SBarry Smith     char   *clen;
2125dc0f0a4SBarry Smith     int    cl;
2135dc0f0a4SBarry Smith     size_t nlen;
2145dc0f0a4SBarry Smith 
2155dc0f0a4SBarry Smith     r = SSL_read(ssl,buff+len,(int)buffsize);
2165dc0f0a4SBarry Smith     len += r;
217b967cddfSBarry Smith     switch (SSL_get_error(ssl,r)){
218b967cddfSBarry Smith     case SSL_ERROR_NONE:
219b967cddfSBarry Smith       break;
220b967cddfSBarry Smith     case SSL_ERROR_ZERO_RETURN:
2215dc0f0a4SBarry Smith       foundbody = PETSC_TRUE;
2225dc0f0a4SBarry Smith       SSL_shutdown(ssl);
223b967cddfSBarry Smith       break;
224b967cddfSBarry Smith     case SSL_ERROR_SYSCALL:
2255dc0f0a4SBarry Smith       foundbody = PETSC_TRUE;
226b967cddfSBarry Smith       break;
227b967cddfSBarry Smith     default:
228b967cddfSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"SSL read problem");
229b967cddfSBarry Smith     }
2305dc0f0a4SBarry Smith 
2315dc0f0a4SBarry Smith     ierr = PetscStrstr(buff,"Content-Length: ",&clen);CHKERRQ(ierr);
2325dc0f0a4SBarry Smith     if (clen) {
2335dc0f0a4SBarry Smith       clen += 15;
2345dc0f0a4SBarry Smith       sscanf(clen,"%d",&cl);
2355dc0f0a4SBarry Smith       if (!cl) foundbody = PETSC_TRUE;
2365dc0f0a4SBarry Smith       else {
2375dc0f0a4SBarry Smith         ierr = PetscStrstr(buff,"\r\n\r\n",&clen);CHKERRQ(ierr);
2385dc0f0a4SBarry Smith         if (clen) {
2395dc0f0a4SBarry Smith           ierr = PetscStrlen(clen,&nlen);CHKERRQ(ierr);
2405dc0f0a4SBarry Smith           if (nlen-4 == (size_t) cl) foundbody = PETSC_TRUE;
2415dc0f0a4SBarry Smith         }
2425dc0f0a4SBarry Smith       }
2435dc0f0a4SBarry Smith     } else {
2445dc0f0a4SBarry Smith       /* if no content length than must leave because you don't know if you can read again */
2455dc0f0a4SBarry Smith       foundbody = PETSC_TRUE;
2465dc0f0a4SBarry Smith     }
2475dc0f0a4SBarry Smith   } while (!foundbody);
248b967cddfSBarry Smith   ierr = PetscInfo1(NULL,"HTTPS result follows: \n%s\n",buff);CHKERRQ(ierr);
249b967cddfSBarry Smith 
250b967cddfSBarry Smith   SSL_free(ssl);
251b967cddfSBarry Smith   ierr = PetscFree(request);CHKERRQ(ierr);
252b967cddfSBarry Smith   PetscFunctionReturn(0);
253b967cddfSBarry Smith }
254b967cddfSBarry Smith 
2554683183fSBarry Smith /*@C
25604102261SBarry Smith      PetscHTTPRequest - Send a request to an HTTP server
25704102261SBarry Smith 
25804102261SBarry Smith    Input Parameters:
25904102261SBarry Smith +   type - either "POST" or "GET"
26004102261SBarry Smith .   url -  URL of request host/path
26104102261SBarry Smith .   header - additional header information, may be NULL
26204102261SBarry Smith .   ctype - data type of body, for example application/json
26304102261SBarry Smith .   body - data to send to server
26404102261SBarry Smith .   sock - obtained with PetscOpenSocket()
26504102261SBarry Smith -   buffsize - size of buffer
26604102261SBarry Smith 
26704102261SBarry Smith    Output Parameter:
26804102261SBarry Smith .   buff - everything returned from server
2694683183fSBarry Smith 
2704683183fSBarry Smith     Level: advanced
2714683183fSBarry Smith 
2724683183fSBarry Smith .seealso: PetscHTTPSRequest(), PetscOpenSocket(), PetscHTTPSConnect(), PetscPullJSONValue()
2734683183fSBarry Smith @*/
27404102261SBarry Smith PetscErrorCode PetscHTTPRequest(const char type[],const char url[],const char header[],const char ctype[],const char body[],int sock,char buff[],size_t buffsize)
27504102261SBarry Smith {
27604102261SBarry Smith   char           *request;
27704102261SBarry Smith   size_t         request_len;
27804102261SBarry Smith   PetscErrorCode ierr;
27904102261SBarry Smith 
28004102261SBarry Smith   PetscFunctionBegin;
28104102261SBarry Smith   ierr = PetscHTTPBuildRequest(type,url,header,ctype,body,&request);CHKERRQ(ierr);
28204102261SBarry Smith   ierr = PetscStrlen(request,&request_len);CHKERRQ(ierr);
28304102261SBarry Smith 
284*f253e43cSLisandro Dalcin   ierr = PetscBinaryWrite(sock,request,request_len,PETSC_CHAR);CHKERRQ(ierr);
28504102261SBarry Smith   ierr = PetscFree(request);CHKERRQ(ierr);
2869860990eSLisandro Dalcin   PetscBinaryRead(sock,buff,buffsize,NULL,PETSC_CHAR);
28704102261SBarry Smith   buff[buffsize-1] = 0;
28804102261SBarry Smith   ierr = PetscInfo1(NULL,"HTTP result follows: \n%s\n",buff);CHKERRQ(ierr);
28904102261SBarry Smith   PetscFunctionReturn(0);
29004102261SBarry Smith }
29104102261SBarry Smith 
2924683183fSBarry Smith /*@C
2934683183fSBarry Smith       PetscHTTPSConnect - connect to a HTTPS server
2944683183fSBarry Smith 
2954683183fSBarry Smith     Input Parameters:
2964683183fSBarry Smith +    host - the name of the machine hosting the HTTPS server
2974683183fSBarry Smith .    port - the port number where the server is hosting, usually 443
2984683183fSBarry Smith -    ctx - value obtained with PetscSSLInitializeContext()
2994683183fSBarry Smith 
3004683183fSBarry Smith     Output Parameters:
3014683183fSBarry Smith +    sock - socket to connect
3024683183fSBarry Smith -    ssl - the argument passed to PetscHTTPSRequest()
3034683183fSBarry Smith 
3044683183fSBarry Smith     Level: advanced
3054683183fSBarry Smith 
3064683183fSBarry Smith .seealso: PetscOpenSocket(), PetscHTTPSRequest(), PetscSSLInitializeContext()
3074683183fSBarry Smith @*/
308b967cddfSBarry Smith PetscErrorCode PetscHTTPSConnect(const char host[],int port,SSL_CTX *ctx,int *sock,SSL **ssl)
309b967cddfSBarry Smith {
310b967cddfSBarry Smith   BIO            *sbio;
311b967cddfSBarry Smith   PetscErrorCode ierr;
312b967cddfSBarry Smith 
313b967cddfSBarry Smith   PetscFunctionBegin;
314b967cddfSBarry Smith   /* Connect the TCP socket*/
315b967cddfSBarry Smith   ierr = PetscOpenSocket(host,port,sock);CHKERRQ(ierr);
316b967cddfSBarry Smith 
317b967cddfSBarry Smith   /* Connect the SSL socket */
318b967cddfSBarry Smith   *ssl = SSL_new(ctx);
319b967cddfSBarry Smith   sbio = BIO_new_socket(*sock,BIO_NOCLOSE);
320b967cddfSBarry Smith   SSL_set_bio(*ssl,sbio,sbio);
321b967cddfSBarry Smith   if (SSL_connect(*ssl) <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"SSL connect error");
322b967cddfSBarry Smith   PetscFunctionReturn(0);
323b967cddfSBarry Smith }
324b967cddfSBarry Smith 
3254683183fSBarry Smith /*@C
3264683183fSBarry Smith      PetscPullJSONValue - Given a JSON response containing the substring with "key" : "value"  where there may or not be spaces around the : returns the value.
3274683183fSBarry Smith 
3284683183fSBarry Smith     Input Parameters:
3294683183fSBarry Smith +    buff - the char array containing the possible values
3304683183fSBarry Smith .    key - the key of the requested value
3314683183fSBarry Smith -    valuelen - the length of the array to contain the value associated with the key
3324683183fSBarry Smith 
3334683183fSBarry Smith     Output Parameters:
3344683183fSBarry Smith +    value - the value obtained
3354683183fSBarry Smith -    found - flag indicating if the value was found in the buff
3364683183fSBarry Smith 
3374683183fSBarry Smith     Level: advanced
3384683183fSBarry Smith 
3394683183fSBarry Smith @*/
34068e69593SBarry Smith PetscErrorCode PetscPullJSONValue(const char buff[],const char key[],char value[],size_t valuelen,PetscBool *found)
34168e69593SBarry Smith {
34268e69593SBarry Smith   PetscErrorCode ierr;
34368e69593SBarry Smith   char           *v,*w;
34468e69593SBarry Smith   char           work[256];
34568e69593SBarry Smith   size_t         len;
34668e69593SBarry Smith 
34768e69593SBarry Smith   PetscFunctionBegin;
34868e69593SBarry Smith   ierr = PetscStrcpy(work,"\"");CHKERRQ(ierr);
349a126751eSBarry Smith   ierr = PetscStrlcat(work,key,sizeof(work));CHKERRQ(ierr);
35068e69593SBarry Smith   ierr = PetscStrcat(work,"\":");CHKERRQ(ierr);
35168e69593SBarry Smith   ierr = PetscStrstr(buff,work,&v);CHKERRQ(ierr);
35268e69593SBarry Smith   ierr = PetscStrlen(work,&len);CHKERRQ(ierr);
35368e69593SBarry Smith   if (v) {
35468e69593SBarry Smith     v += len;
35568e69593SBarry Smith   } else {
35668e69593SBarry Smith     work[len++-1] = 0;
35768e69593SBarry Smith     ierr = PetscStrcat(work," :");CHKERRQ(ierr);
35868e69593SBarry Smith     ierr = PetscStrstr(buff,work,&v);CHKERRQ(ierr);
35968e69593SBarry Smith     if (!v) {
36068e69593SBarry Smith       *found = PETSC_FALSE;
36168e69593SBarry Smith       PetscFunctionReturn(0);
36268e69593SBarry Smith     }
36368e69593SBarry Smith     v += len;
36468e69593SBarry Smith   }
36568e69593SBarry Smith   ierr = PetscStrchr(v,'\"',&v);CHKERRQ(ierr);
36668e69593SBarry Smith   if (!v) {
36768e69593SBarry Smith     *found = PETSC_FALSE;
36868e69593SBarry Smith     PetscFunctionReturn(0);
36968e69593SBarry Smith   }
37068e69593SBarry Smith   ierr = PetscStrchr(v+1,'\"',&w);CHKERRQ(ierr);
37168e69593SBarry Smith   if (!w) {
37268e69593SBarry Smith     *found = PETSC_FALSE;
37368e69593SBarry Smith     PetscFunctionReturn(0);
37468e69593SBarry Smith   }
37568e69593SBarry Smith   *found = PETSC_TRUE;
376c1f4622dSBarry Smith   ierr = PetscStrncpy(value,v+1,PetscMin((size_t)(w-v),valuelen));CHKERRQ(ierr);
37768e69593SBarry Smith   PetscFunctionReturn(0);
37868e69593SBarry Smith }
3795dc0f0a4SBarry Smith 
3805dc0f0a4SBarry Smith #include <ctype.h>
3815dc0f0a4SBarry Smith 
3824683183fSBarry Smith /*@C
3834683183fSBarry Smith     PetscPushJSONValue -  Puts a "key" : "value" pair onto a string
3845dc0f0a4SBarry Smith 
3854683183fSBarry Smith     Input Parameters:
3864683183fSBarry Smith +   buffer - the char array where the value will be put
3874683183fSBarry Smith .   key - the key value to be set
3884683183fSBarry Smith .   value - the value associated with the key
3894683183fSBarry Smith -   bufflen - the size of the buffer (currently ignored)
3904683183fSBarry Smith 
3914683183fSBarry Smith     Level: advanced
3924683183fSBarry Smith 
39395452b02SPatrick Sanan     Notes:
39495452b02SPatrick Sanan     Ignores lengths so can cause buffer overflow
3954683183fSBarry Smith @*/
3965dc0f0a4SBarry Smith PetscErrorCode PetscPushJSONValue(char buff[],const char key[],const char value[],size_t bufflen)
3975dc0f0a4SBarry Smith {
3985dc0f0a4SBarry Smith   PetscErrorCode ierr;
3995dc0f0a4SBarry Smith   size_t         len;
4005dc0f0a4SBarry Smith   PetscBool      special;
4015dc0f0a4SBarry Smith 
4025dc0f0a4SBarry Smith   PetscFunctionBegin;
4035dc0f0a4SBarry Smith   ierr = PetscStrcmp(value,"null",&special);CHKERRQ(ierr);
4045dc0f0a4SBarry Smith   if (!special) {
4055dc0f0a4SBarry Smith     ierr = PetscStrcmp(value,"true",&special);CHKERRQ(ierr);
4065dc0f0a4SBarry Smith   }
4075dc0f0a4SBarry Smith   if (!special) {
4085dc0f0a4SBarry Smith     ierr = PetscStrcmp(value,"false",&special);CHKERRQ(ierr);
4095dc0f0a4SBarry Smith   }
4105dc0f0a4SBarry Smith   if (!special) {
4115dc0f0a4SBarry Smith     PetscInt i;
4125dc0f0a4SBarry Smith 
4135dc0f0a4SBarry Smith     ierr    = PetscStrlen(value,&len);CHKERRQ(ierr);
4145dc0f0a4SBarry Smith     special = PETSC_TRUE;
4155dc0f0a4SBarry Smith     for (i=0; i<(int)len; i++) {
4165dc0f0a4SBarry Smith       if (!isdigit(value[i])) {
4175dc0f0a4SBarry Smith         special = PETSC_FALSE;
4185dc0f0a4SBarry Smith         break;
4195dc0f0a4SBarry Smith       }
4205dc0f0a4SBarry Smith     }
4215dc0f0a4SBarry Smith   }
4225dc0f0a4SBarry Smith 
4235dc0f0a4SBarry Smith   ierr = PetscStrcat(buff,"\"");CHKERRQ(ierr);
4245dc0f0a4SBarry Smith   ierr = PetscStrcat(buff,key);CHKERRQ(ierr);
4255dc0f0a4SBarry Smith   ierr = PetscStrcat(buff,"\":");CHKERRQ(ierr);
4265dc0f0a4SBarry Smith   if (!special) {
4275dc0f0a4SBarry Smith     ierr = PetscStrcat(buff,"\"");CHKERRQ(ierr);
4285dc0f0a4SBarry Smith   }
4295dc0f0a4SBarry Smith   ierr = PetscStrcat(buff,value);CHKERRQ(ierr);
4305dc0f0a4SBarry Smith   if (!special) {
4315dc0f0a4SBarry Smith     ierr = PetscStrcat(buff,"\"");CHKERRQ(ierr);
4325dc0f0a4SBarry Smith   }
4335dc0f0a4SBarry Smith   PetscFunctionReturn(0);
4345dc0f0a4SBarry Smith }
435