Skip to content

Commit

Permalink
Merge pull request #15 from 1000TurquoisePogs/feature/no-http-caching
Browse files Browse the repository at this point in the history
Add convenience function to roll up headers used for REST JSON responses
  • Loading branch information
1000TurquoisePogs authored Feb 27, 2019
2 parents 1e4cc49 + 07517b4 commit e8ec831
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
1 change: 0 additions & 1 deletion c/dataservice.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ int makeHttpDataServiceUrlMask(DataService *dataService, char *urlMaskBuffer, in
return 0;
}


/*
This program and the accompanying materials are
made available under the terms of the Eclipse Public License v2.0 which accompanies
Expand Down
17 changes: 5 additions & 12 deletions c/datasetjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -1032,9 +1032,7 @@ void respondWithDataset(HttpResponse* response, char* absolutePath, int jsonMode

jsonPrinter *jPrinter = respondWithJsonPrinter(response);
setResponseStatus(response, 200, "OK");
setContentType(response, "text/json");
addStringHeader(response,"Server","jdmfws");
addStringHeader(response,"Transfer-Encoding","chunked");
setDefaultJSONRESTHeaders(response);

writeHeader(response);

Expand Down Expand Up @@ -1316,9 +1314,8 @@ void respondWithVSAMDataset(HttpResponse* response, char* absolutePath, hashtabl

jsonPrinter *jPrinter = respondWithJsonPrinter(response);
setResponseStatus(response, 200, "OK");
setContentType(response, "text/json");
addStringHeader(response,"Server","jdmfws");
addStringHeader(response,"Transfer-Encoding","chunked");
setDefaultJSONRESTHeaders(response);

writeHeader(response);

printf("Streaming data for %s\n", absolutePath);
Expand Down Expand Up @@ -1482,9 +1479,7 @@ void respondWithDatasetMetadata(HttpResponse *response) {

jsonPrinter *jPrinter = respondWithJsonPrinter(response);
setResponseStatus(response, 200, "OK");
setContentType(response, "text/json");
addStringHeader(response, "Server", "jdmfws");
addStringHeader(response, "Transfer-Encoding", "chunked");
setDefaultJSONRESTHeaders(response);
writeHeader(response);
char volser[7];
memset(volser,0,7);
Expand Down Expand Up @@ -1574,9 +1569,7 @@ void respondWithHLQNames(HttpResponse *response, MetadataQueryCache *metadataQue
#ifdef __ZOWE_OS_ZOS
HttpRequest *request = response->request;
setResponseStatus(response, 200, "OK");
setContentType(response, "text/json");
addStringHeader(response, "Server", "jdmfws");
addStringHeader(response, "Transfer-Encoding", "chunked");
setDefaultJSONRESTHeaders(response);
jsonPrinter *jPrinter = respondWithJsonPrinter(response);
writeHeader(response);
EntryDataSet *hlqSet;
Expand Down
8 changes: 2 additions & 6 deletions c/httpfileservice.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@

void response200WithMessage(HttpResponse *response, char *msg) {
setResponseStatus(response,200,"OK");
addStringHeader(response, "Server", "jdmfws");
setContentType(response, "text/json");
addStringHeader(response,"Transfer-Encoding","chunked");
setDefaultJSONRESTHeaders(response);
addStringHeader(response,"Connection","close");
writeHeader(response);
jsonPrinter *out = respondWithJsonPrinter(response);
Expand Down Expand Up @@ -560,9 +558,7 @@ void respondWithUnixFileMetadata(HttpResponse *response, char *absolutePath) {
jsonPrinter *out = respondWithJsonPrinter(response);

setResponseStatus(response, 200, "OK");
setContentType(response, "text/json");
addStringHeader(response, "Server", "jdmfws");
addStringHeader(response, "Transfer-Encoding", "chunked");
setDefaultJSONRESTHeaders(response);
writeHeader(response);

int decimalMode = fileUnixMode(&info);
Expand Down
20 changes: 16 additions & 4 deletions c/httpserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -3635,6 +3635,8 @@ void respondWithUnixFile2(HttpService* service, HttpResponse* response, char* ab

if (!modified) {
setResponseStatus(response, 304, "Not modified");
addStringHeader(response, "Cache-control", "no-store");
addStringHeader(response, "Pragma", "no-cache");
addStringHeader(response, "Server", "jdmfws");
addCacheRelatedHeaders(response, mtime, etag);
writeHeader(response);
Expand Down Expand Up @@ -3670,6 +3672,8 @@ void respondWithUnixFile2(HttpService* service, HttpResponse* response, char* ab

setResponseStatus(response,200,"OK");
addStringHeader(response,"Server","jdmfws");
addStringHeader(response, "Cache-control", "no-store");
addStringHeader(response, "Pragma", "no-cache");
addIntHeader(response,"Content-Length",fileSize); /* Is this safe post-conversion??? */
setContentType(response, mimeType);
addCacheRelatedHeaders(response, mtime, etag);
Expand Down Expand Up @@ -3753,6 +3757,8 @@ void respondWithUnixDirectory(HttpResponse *response, char* absolutePath, int js
fflush(stdout);
#endif
setResponseStatus(response,200,"OK");
addStringHeader(response, "Cache-control", "no-store");
addStringHeader(response, "Pragma", "no-cache");
addStringHeader(response,"Server","jdmfws");
addStringHeader(response,"Transfer-Encoding","chunked");
if (jsonMode == 0) {
Expand Down Expand Up @@ -3782,6 +3788,8 @@ void respondWithUnixFileNotFound(HttpResponse* response, int jsonMode) {
addStringHeader(response,"Server","jdmfws");
setContentType(response,"text/plain");
setResponseStatus(response,404,"Not Found");
addStringHeader(response, "Cache-control", "no-store");
addStringHeader(response, "Pragma", "no-cache");
addIntHeader(response,"Content-Length",len);
writeHeader(response);

Expand All @@ -3796,15 +3804,19 @@ void respondWithUnixFileNotFound(HttpResponse* response, int jsonMode) {
}
}

void setDefaultJSONRESTHeaders(HttpResponse *response) {
setContentType(response, "application/json");
addStringHeader(response, "Server", "jdmfws");
addStringHeader(response, "Transfer-Encoding", "chunked");
addStringHeader(response, "Cache-control", "no-store");
addStringHeader(response, "Pragma", "no-cache");
}

// Response must ALWAYS be finished on return
void respondWithJsonError(HttpResponse *response, char *error, int statusCode, char *statusMessage) {
jsonPrinter *out = respondWithJsonPrinter(response);

setContentType(response, "text/json");
setResponseStatus(response,statusCode,statusMessage);
addStringHeader(response, "Server", "jdmfws");
addStringHeader(response, "Transfer-Encoding", "chunked");
setDefaultJSONRESTHeaders(response);
writeHeader(response);

jsonStart(out);
Expand Down
2 changes: 2 additions & 0 deletions h/dataservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ void initalizeWebPlugin(WebPlugin *plugin, HttpServer *server);
HttpService *makeHttpDataService(DataService *dataService, HttpServer *server);
int makeHttpDataServiceUrlMask(DataService *dataService, char *urlMaskBuffer, int urlMaskBufferSize, char *productPrefix);



#endif /* __DATASERVICE__ */


Expand Down
5 changes: 4 additions & 1 deletion h/httpserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,10 @@ int streamTextForFile(Socket *socket, UnixFile *in, int encoding,
int makeHTMLForDirectory(HttpResponse *response, char *dirname, char *stem, int includeDotted);
int makeJSONForDirectory(HttpResponse *response, char *dirname, int includeDotted);


/**
Convenience function to set headers specific to sending small JSON objects for a REST API
*/
void setDefaultJSONRESTHeaders(HttpResponse *response);

int setHttpParseTrace(int toWhat);
int setHttpDispatchTrace(int toWhat);
Expand Down

0 comments on commit e8ec831

Please sign in to comment.