10efc6a03SBarry Smith 20efc6a03SBarry Smith #include <petscwebclient.h> 30efc6a03SBarry Smith 40efc6a03SBarry Smith /* 50efc6a03SBarry Smith These variables identify the code as a PETSc application to Google. 60efc6a03SBarry Smith 70efc6a03SBarry Smith See - http://stackoverflow.com/questions/4616553/using-oauth-in-free-open-source-software 80efc6a03SBarry Smith Users can get their own application IDs - https://code.google.com/p/google-apps-manager/wiki/GettingAnOAuthConsoleKey 90efc6a03SBarry Smith 100efc6a03SBarry Smith */ 110efc6a03SBarry Smith #define PETSC_GOOGLE_CLIENT_ID "521429262559-i19i57eek8tnt9ftpp4p91rcl0bo9ag5.apps.googleusercontent.com" 120efc6a03SBarry Smith #define PETSC_GOOGLE_CLIENT_ST "vOds_A71I3_S_aHMq_kZAI0t" 130efc6a03SBarry Smith 140efc6a03SBarry Smith 150efc6a03SBarry Smith #undef __FUNCT__ 160efc6a03SBarry Smith #define __FUNCT__ "PetscGoogleDriveRefresh" 170efc6a03SBarry Smith /*@C 180efc6a03SBarry Smith PetscGoogleDriveRefresh - Get a new authorization token for accessing Google drive from PETSc from a refresh token 190efc6a03SBarry Smith 200efc6a03SBarry Smith Not collective, only the first process in the MPI_Comm does anything 210efc6a03SBarry Smith 220efc6a03SBarry Smith Input Parameters: 230efc6a03SBarry Smith + comm - MPI communicator 240efc6a03SBarry Smith . refresh token - obtained with PetscGoogleDriveAuthorize(), if NULL PETSc will first look for one in the options data 250efc6a03SBarry Smith if not found it will call PetscGoogleDriveAuthorize() 260efc6a03SBarry Smith - tokensize - size of the output string access_token 270efc6a03SBarry Smith 280efc6a03SBarry Smith Output Parameter: 290efc6a03SBarry Smith . access_token - token that can be passed to PetscGoogleDriveUpload() 300efc6a03SBarry Smith 314a285bdaSBarry Smith Options Database: 324a285bdaSBarry Smith . -google_refresh_token XXX where XXX was obtained from PetscGoogleDriveAuthorize() 334a285bdaSBarry Smith 344a285bdaSBarry Smith 350efc6a03SBarry Smith .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveUpload() 360efc6a03SBarry Smith 370efc6a03SBarry Smith @*/ 380efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm comm,const char refresh_token[],char access_token[],size_t tokensize) 390efc6a03SBarry Smith { 400efc6a03SBarry Smith SSL_CTX *ctx; 410efc6a03SBarry Smith SSL *ssl; 420efc6a03SBarry Smith int sock; 430efc6a03SBarry Smith PetscErrorCode ierr; 440efc6a03SBarry Smith char buff[8*1024],body[1024],*access,*ctmp; 450efc6a03SBarry Smith PetscMPIInt rank; 460efc6a03SBarry Smith char *refreshtoken = (char*)refresh_token; 470efc6a03SBarry Smith 480efc6a03SBarry Smith PetscFunctionBegin; 490efc6a03SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 500efc6a03SBarry Smith if (!rank) { 510efc6a03SBarry Smith if (!refresh_token) { 520efc6a03SBarry Smith PetscBool set; 530efc6a03SBarry Smith ierr = PetscMalloc1(512,&refreshtoken);CHKERRQ(ierr); 544a285bdaSBarry Smith ierr = PetscOptionsGetString(NULL,"-google_refresh_token",refreshtoken,512,&set);CHKERRQ(ierr); 550efc6a03SBarry Smith if (!set) { 560efc6a03SBarry Smith ierr = PetscGoogleDriveAuthorize(comm,access_token,refreshtoken,512*sizeof(char));CHKERRQ(ierr); 570efc6a03SBarry Smith ierr = PetscFree(refreshtoken);CHKERRQ(ierr); 580efc6a03SBarry Smith PetscFunctionReturn(0); 590efc6a03SBarry Smith } 600efc6a03SBarry Smith } 610efc6a03SBarry Smith ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); 620efc6a03SBarry Smith ierr = PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); 634a285bdaSBarry Smith ierr = PetscStrcpy(body,"client_id=");CHKERRQ(ierr); 640efc6a03SBarry Smith ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);CHKERRQ(ierr); 650efc6a03SBarry Smith ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr); 660efc6a03SBarry Smith ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);CHKERRQ(ierr); 670efc6a03SBarry Smith ierr = PetscStrcat(body,"&refresh_token=");CHKERRQ(ierr); 680efc6a03SBarry Smith ierr = PetscStrcat(body,refreshtoken);CHKERRQ(ierr); 690efc6a03SBarry Smith if (!refresh_token) {ierr = PetscFree(refreshtoken);CHKERRQ(ierr);} 700efc6a03SBarry Smith ierr = PetscStrcat(body,"&grant_type=refresh_token");CHKERRQ(ierr); 710efc6a03SBarry Smith 7293e1d32fSBarry Smith ierr = PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); 730efc6a03SBarry Smith ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); 740efc6a03SBarry Smith close(sock); 750efc6a03SBarry Smith 760efc6a03SBarry Smith ierr = PetscStrstr(buff,"\"access_token\" : \"",&access);CHKERRQ(ierr); 770efc6a03SBarry Smith if (!access) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Did not receive access token from Google"); 780efc6a03SBarry Smith access += 18; 790efc6a03SBarry Smith ierr = PetscStrchr(access,'\"',&ctmp);CHKERRQ(ierr); 800efc6a03SBarry Smith if (!ctmp) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Access token from Google is misformed"); 810efc6a03SBarry Smith *ctmp = 0; 820efc6a03SBarry Smith ierr = PetscStrncpy(access_token,access,tokensize);CHKERRQ(ierr); 830efc6a03SBarry Smith *ctmp = '\"'; 840efc6a03SBarry Smith } 850efc6a03SBarry Smith PetscFunctionReturn(0); 860efc6a03SBarry Smith } 870efc6a03SBarry Smith 880efc6a03SBarry Smith #include <sys/stat.h> 890efc6a03SBarry Smith 900efc6a03SBarry Smith #undef __FUNCT__ 910efc6a03SBarry Smith #define __FUNCT__ "PetscGoogleDriveUpload" 920efc6a03SBarry Smith /*@C 930efc6a03SBarry Smith PetscGoogleDriveUpload - Loads a file to the Google Drive 940efc6a03SBarry Smith 950efc6a03SBarry Smith Not collective, only the first process in the MPI_Comm uploads the file 960efc6a03SBarry Smith 970efc6a03SBarry Smith Input Parameters: 980efc6a03SBarry Smith + comm - MPI communicator 990efc6a03SBarry Smith . access_token - obtained with PetscGoogleDriveRefresh(), pass NULL to have PETSc generate one 1000efc6a03SBarry Smith - filename - file to upload; if you upload multiple times it will have different names each time on Google Drive 1010efc6a03SBarry Smith 1020efc6a03SBarry Smith Options Database: 1034a285bdaSBarry Smith . -google_refresh_token XXX 1040efc6a03SBarry Smith 1050efc6a03SBarry Smith Usage Patterns: 1064a285bdaSBarry Smith With PETSc option -google_refresh_token XXX given 1070efc6a03SBarry Smith PetscGoogleDriveUpload(comm,NULL,filename); will upload file with no user interaction 1080efc6a03SBarry Smith 1094a285bdaSBarry Smith Without PETSc option -google_refresh_token XXX given 1100efc6a03SBarry Smith PetscGoogleDriveUpload(comm,NULL,filename); for first use will prompt user to authorize access to Google Drive with their processor 1110efc6a03SBarry Smith 1124a285bdaSBarry Smith With PETSc option -google_refresh_token XXX given 1130efc6a03SBarry Smith PetscGoogleDriveRefresh(comm,NULL,access_token,sizeof(access_token)); 1140efc6a03SBarry Smith PetscGoogleDriveUpload(comm,access_token,filename); 1150efc6a03SBarry Smith 1160efc6a03SBarry Smith With refresh token entered in some way by the user 1170efc6a03SBarry Smith PetscGoogleDriveRefresh(comm,refresh_token,access_token,sizeof(access_token)); 1180efc6a03SBarry Smith PetscGoogleDriveUpload(comm,access_token,filename); 1190efc6a03SBarry Smith 1200efc6a03SBarry Smith PetscGoogleDriveAuthorize(comm,access_token,refresh_token,sizeof(access_token)); 1210efc6a03SBarry Smith PetscGoogleDriveUpload(comm,access_token,filename); 1220efc6a03SBarry Smith 1230efc6a03SBarry Smith .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveRefresh() 1240efc6a03SBarry Smith 1250efc6a03SBarry Smith @*/ 1260efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveUpload(MPI_Comm comm,const char access_token[],const char filename[]) 1270efc6a03SBarry Smith { 1280efc6a03SBarry Smith SSL_CTX *ctx; 1290efc6a03SBarry Smith SSL *ssl; 1300efc6a03SBarry Smith int sock; 1310efc6a03SBarry Smith PetscErrorCode ierr; 1320efc6a03SBarry Smith char head[1024],buff[8*1024],*body,*title; 1330efc6a03SBarry Smith PetscMPIInt rank; 1340efc6a03SBarry Smith struct stat sb; 1350efc6a03SBarry Smith size_t len,blen,rd; 1360efc6a03SBarry Smith FILE *fd; 1370efc6a03SBarry Smith 1380efc6a03SBarry Smith PetscFunctionBegin; 1390efc6a03SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 1400efc6a03SBarry Smith if (!rank) { 1410efc6a03SBarry Smith ierr = PetscStrcpy(head,"Authorization: Bearer ");CHKERRQ(ierr); 1420efc6a03SBarry Smith ierr = PetscStrcat(head,access_token);CHKERRQ(ierr); 1430efc6a03SBarry Smith ierr = PetscStrcat(head,"\r\n");CHKERRQ(ierr); 1440efc6a03SBarry Smith ierr = PetscStrcat(head,"uploadType: multipart\r\n");CHKERRQ(ierr); 1450efc6a03SBarry Smith 1460efc6a03SBarry Smith ierr = stat(filename,&sb); 1470efc6a03SBarry Smith if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to stat file: %s",filename); 1480efc6a03SBarry Smith len = 1024 + sb.st_size; 1490efc6a03SBarry Smith ierr = PetscMalloc1(len,&body);CHKERRQ(ierr); 1500efc6a03SBarry Smith ierr = PetscStrcpy(body,"--foo_bar_baz\r\n" 1510efc6a03SBarry Smith "Content-Type: application/json\r\n\r\n" 1520efc6a03SBarry Smith "{" 1530efc6a03SBarry Smith "\"title\": \""); 1540efc6a03SBarry Smith ierr = PetscStrcat(body,filename); 1550efc6a03SBarry Smith ierr = PetscStrcat(body,"\"," 1560efc6a03SBarry Smith "\"mimeType\": \"text.html\"," 1570efc6a03SBarry Smith "\"description\": \" a file\"" 1580efc6a03SBarry Smith "}\r\n\r\n" 1590efc6a03SBarry Smith "--foo_bar_baz\r\n" 1600efc6a03SBarry Smith "Content-Type: text/html\r\n\r\n"); 1610efc6a03SBarry Smith ierr = PetscStrlen(body,&blen);CHKERRQ(ierr); 1620efc6a03SBarry Smith fd = fopen (filename, "r"); 1630efc6a03SBarry Smith if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open file: %s",filename); 1640efc6a03SBarry Smith rd = fread (body+blen, sizeof (unsigned char), sb.st_size, fd); 165d8dcb26dSBarry Smith if (rd != (size_t) sb.st_size) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to read entire file: %s %d %d",filename,(int)rd,sb.st_size); 1660efc6a03SBarry Smith fclose(fd); 1670efc6a03SBarry Smith body[blen + rd] = 0; 1680efc6a03SBarry Smith ierr = PetscStrcat(body,"\r\n\r\n" 1690efc6a03SBarry Smith "--foo_bar_baz\r\n"); 1700efc6a03SBarry Smith ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); 1710efc6a03SBarry Smith ierr = PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); 17293e1d32fSBarry Smith ierr = PetscHTTPSRequest("POST","www.googleapis.com/upload/drive/v2/files/",head,"multipart/related; boundary=\"foo_bar_baz\"",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); 1730efc6a03SBarry Smith ierr = PetscFree(body);CHKERRQ(ierr); 1740efc6a03SBarry Smith ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); 1750efc6a03SBarry Smith close(sock); 1760efc6a03SBarry Smith ierr = PetscStrstr(buff,"\"title\"",&title);CHKERRQ(ierr); 1770efc6a03SBarry Smith if (!title) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Upload of file %s failed",filename); 1780efc6a03SBarry Smith } 1790efc6a03SBarry Smith PetscFunctionReturn(0); 1800efc6a03SBarry Smith } 1810efc6a03SBarry Smith 182*68e69593SBarry Smith #if defined(PETSC_HAVE_UNISTD_H) 183*68e69593SBarry Smith #include <unistd.h> 184*68e69593SBarry Smith #endif 185*68e69593SBarry Smith 1860efc6a03SBarry Smith #undef __FUNCT__ 1870efc6a03SBarry Smith #define __FUNCT__ "PetscGoogleDriveAuthorize" 1880efc6a03SBarry Smith /*@C 1890efc6a03SBarry Smith PetscGoogleDriveAuthorize - Get authorization and refresh token for accessing Google drive from PETSc 1900efc6a03SBarry Smith 1910efc6a03SBarry Smith Not collective, only the first process in MPI_Comm does anything 1920efc6a03SBarry Smith 1930efc6a03SBarry Smith Input Parameters: 1940efc6a03SBarry Smith + comm - the MPI communicator 1950efc6a03SBarry Smith - tokensize - size of the token arrays 1960efc6a03SBarry Smith 1970efc6a03SBarry Smith Output Parameters: 1980efc6a03SBarry Smith + access_token - can be used with PetscGoogleDriveUpload() for this one session 1990efc6a03SBarry Smith - refresh_token - can be used for ever to obtain new access_tokens with PetscGoogleDriveRefresh(), guard this like a password 2000efc6a03SBarry Smith it gives access to your Google Drive 2010efc6a03SBarry Smith 2020efc6a03SBarry Smith Notes: This call requires stdout and stdin access from process 0 on the MPI communicator 2030efc6a03SBarry Smith 204c245270aSBarry Smith You can run src/sys/webclient/examples/tutorials/googleobtainrefreshtoken to get a refresh token and then in the future pass it to 2054a285bdaSBarry Smith PETSc programs with -google_refresh_token XXX 2060efc6a03SBarry Smith 2070efc6a03SBarry Smith .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscURLShorten() 2080efc6a03SBarry Smith 2090efc6a03SBarry Smith @*/ 2100efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm comm,char access_token[],char refresh_token[],size_t tokensize) 2110efc6a03SBarry Smith { 2120efc6a03SBarry Smith SSL_CTX *ctx; 2130efc6a03SBarry Smith SSL *ssl; 2140efc6a03SBarry Smith int sock; 2150efc6a03SBarry Smith PetscErrorCode ierr; 2160efc6a03SBarry Smith char buff[8*1024],*ptr,body[1024],*access,*refresh,*ctmp; 2170efc6a03SBarry Smith PetscMPIInt rank; 2180efc6a03SBarry Smith size_t len; 2190efc6a03SBarry Smith 2200efc6a03SBarry Smith PetscFunctionBegin; 221*68e69593SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 222*68e69593SBarry Smith if (!rank) { 223*68e69593SBarry Smith if (!isatty(fileno(PETSC_STDOUT))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Requires users input/output"); 2244a285bdaSBarry Smith ierr = PetscPrintf(comm,"Cut and paste the following into your browser:\n\n" 2250efc6a03SBarry Smith "https://accounts.google.com/o/oauth2/auth?" 2260efc6a03SBarry Smith "scope=https%%3A%%2F%%2Fwww.googleapis.com%%2Fauth%%2Fdrive.file&" 2270efc6a03SBarry Smith "redirect_uri=urn:ietf:wg:oauth:2.0:oob&" 2280efc6a03SBarry Smith "response_type=code&" 2290efc6a03SBarry Smith "client_id=" 2300efc6a03SBarry Smith PETSC_GOOGLE_CLIENT_ID 2310efc6a03SBarry Smith "\n\n");CHKERRQ(ierr); 2320efc6a03SBarry Smith ierr = PetscPrintf(comm,"Paste the result here:");CHKERRQ(ierr); 2330efc6a03SBarry Smith ptr = fgets(buff, 1024, stdin); 2340efc6a03SBarry Smith if (!ptr) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno); 2350efc6a03SBarry Smith ierr = PetscStrlen(buff,&len);CHKERRQ(ierr); 2360efc6a03SBarry Smith buff[len-1] = 0; /* remove carriage return at end of line */ 2370efc6a03SBarry Smith 2380efc6a03SBarry Smith ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); 2390efc6a03SBarry Smith ierr = PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); 2400efc6a03SBarry Smith ierr = PetscStrcpy(body,"code=");CHKERRQ(ierr); 2410efc6a03SBarry Smith ierr = PetscStrcat(body,buff);CHKERRQ(ierr); 2420efc6a03SBarry Smith ierr = PetscStrcat(body,"&client_id=");CHKERRQ(ierr); 2430efc6a03SBarry Smith ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);CHKERRQ(ierr); 2440efc6a03SBarry Smith ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr); 2450efc6a03SBarry Smith ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);CHKERRQ(ierr); 2460efc6a03SBarry Smith ierr = PetscStrcat(body,"&redirect_uri=urn:ietf:wg:oauth:2.0:oob&");CHKERRQ(ierr); 2470efc6a03SBarry Smith ierr = PetscStrcat(body,"grant_type=authorization_code");CHKERRQ(ierr); 2480efc6a03SBarry Smith 24993e1d32fSBarry Smith ierr = PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); 2500efc6a03SBarry Smith ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); 2510efc6a03SBarry Smith close(sock); 2520efc6a03SBarry Smith 2530efc6a03SBarry Smith ierr = PetscStrstr(buff,"\"access_token\" : \"",&access);CHKERRQ(ierr); 2540efc6a03SBarry Smith if (!access) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Did not receive access token from Google"); 2550efc6a03SBarry Smith access += 18; 2560efc6a03SBarry Smith ierr = PetscStrchr(access,'\"',&ctmp);CHKERRQ(ierr); 2570efc6a03SBarry Smith if (!ctmp) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Access token from Google is misformed"); 2580efc6a03SBarry Smith *ctmp = 0; 2590efc6a03SBarry Smith ierr = PetscStrncpy(access_token,access,tokensize);CHKERRQ(ierr); 2600efc6a03SBarry Smith *ctmp = '\"'; 2610efc6a03SBarry Smith 2620efc6a03SBarry Smith ierr = PetscStrstr(buff,"\"refresh_token\" : \"",&refresh);CHKERRQ(ierr); 2630efc6a03SBarry Smith if (!refresh) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Did not receive refresh token from Google"); 2640efc6a03SBarry Smith refresh += 19; 2650efc6a03SBarry Smith ierr = PetscStrchr(refresh,'\"',&ctmp);CHKERRQ(ierr); 2660efc6a03SBarry Smith if (!ctmp) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Refresh token from Google is misformed"); 2670efc6a03SBarry Smith *ctmp = 0; 2680efc6a03SBarry Smith ierr = PetscStrncpy(refresh_token,refresh,tokensize);CHKERRQ(ierr); 2690efc6a03SBarry Smith 2700efc6a03SBarry Smith ierr = PetscPrintf(comm,"Here is your Google refresh token, save it in a save place, in the future you can run PETSc\n");CHKERRQ(ierr); 271c245270aSBarry Smith ierr = PetscPrintf(comm,"programs with the option -google_refresh_token %s\n",refresh);CHKERRQ(ierr); 2720efc6a03SBarry Smith ierr = PetscPrintf(comm,"to access Google Drive automatically\n");CHKERRQ(ierr); 2730efc6a03SBarry Smith } 2740efc6a03SBarry Smith PetscFunctionReturn(0); 2750efc6a03SBarry Smith } 2760efc6a03SBarry Smith 2770efc6a03SBarry Smith 2780efc6a03SBarry Smith #undef __FUNCT__ 2790efc6a03SBarry Smith #define __FUNCT__ "PetscURLShorten" 2800efc6a03SBarry Smith /*@C 2810efc6a03SBarry Smith PetscURLShorten - Uses Google's service to get a short url for a long url 2820efc6a03SBarry Smith 2830efc6a03SBarry Smith Input Parameters: 2840efc6a03SBarry Smith + url - long URL you want shortened 2850efc6a03SBarry Smith - lenshorturl - length of buffer to contain short URL 2860efc6a03SBarry Smith 2870efc6a03SBarry Smith Output Parameter: 2880efc6a03SBarry Smith . shorturl - the shortened URL 2890efc6a03SBarry Smith 2900efc6a03SBarry Smith .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscGoogleDriveAuthorize() 2910efc6a03SBarry Smith @*/ 2920efc6a03SBarry Smith PetscErrorCode PetscURLShorten(const char url[],char shorturl[],size_t lenshorturl) 2930efc6a03SBarry Smith { 2940efc6a03SBarry Smith SSL_CTX *ctx; 2950efc6a03SBarry Smith SSL *ssl; 2960efc6a03SBarry Smith int sock; 2970efc6a03SBarry Smith PetscErrorCode ierr; 2980efc6a03SBarry Smith char buff[1024],body[512],*sub1,*sub2; 2990efc6a03SBarry Smith 3000efc6a03SBarry Smith PetscFunctionBegin; 3010efc6a03SBarry Smith ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); 3020efc6a03SBarry Smith ierr = PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); 3030efc6a03SBarry Smith ierr = PetscSNPrintf(body,512,"{\"longUrl\": \"%s\"}",url);CHKERRQ(ierr); 30493e1d32fSBarry Smith ierr = PetscHTTPSRequest("POST","www.googleapis.com/urlshortener/v1/url",NULL,"application/json",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); 3050efc6a03SBarry Smith ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); 3060efc6a03SBarry Smith close(sock); 3070efc6a03SBarry Smith ierr = PetscStrstr(buff,"\"id\": \"",&sub1);CHKERRQ(ierr); 3080efc6a03SBarry Smith if (sub1) { 3090efc6a03SBarry Smith sub1 += 7; 3100efc6a03SBarry Smith ierr = PetscStrstr(sub1,"\"",&sub2);CHKERRQ(ierr); 3110efc6a03SBarry Smith if (!sub2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google did not shorten URL"); 3120efc6a03SBarry Smith sub2[0] = 0; 3130efc6a03SBarry Smith ierr = PetscStrncpy(shorturl,sub1,lenshorturl);CHKERRQ(ierr); 3140efc6a03SBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google did not shorten URL"); 3150efc6a03SBarry Smith PetscFunctionReturn(0); 3160efc6a03SBarry Smith } 3170efc6a03SBarry Smith 318