-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
executable file
·919 lines (800 loc) · 29.6 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
#!/usr/bin/env node
const path = require('path')
const moment = require('moment') // Time library http://moment.js
const compress = require('compression') // Express compression module
const express = require('express') // Client/server library
const app = express()
const semver = require('semver')
// Object that contains all metadata
const metadata = require('./lib/metadata.js').metadata
// Prepare metadata and store in metadata object
const prepmetadata = require('./lib/metadata.js').prepmetadata
const executeCommand = require('./lib/executeCommand.js')
// Command line interface
const argv = require('./lib/cli.js').argv
// Logging
const log = require('log')
log.logDir = argv.logdir
log.logLevel = argv.loglevel
log.info('Relative path in log messages base dir')
log.info(` ${__dirname}`)
exceptions() // Catch uncaught exceptions
// Get default configuration variables stored in ./conf/server.json
const config = require('./lib/conf.js').configVars(argv)
for (const key in config) {
log.info(key + ' = ' + config[key])
}
// Handle file patterns.
const files = require('./lib/expandglob.js').expandglob(argv.file)
// Populate metadata.cache array, which has elements of catalog objects.
// main() is callback.
prepmetadata(files, argv.force, argv.ignore, argv.skipchecks, main)
function main () {
// setInterval(() => { log.memory() }, 1000)
// Compress responses using gzip
app.use(compress())
// Serve static files in ./public/data (no directory listing provided)
app.use('/data', express.static(path.join(__dirname, 'public', 'data')))
const catalogs = []
const prefixes = []
const localAll = []
for (const key in metadata.cache) {
const catalog = metadata.cache[key].server.id
const prefix = metadata.cache[key].server.prefix
const server = metadata(catalog, 'server')
const data = metadata(catalog, 'data')
localAll.push(`${prefix}/hapi,${catalog},${catalog},${server.contact},${data.contact}\n`)
catalogs.push(catalog)
prefixes.push(prefix)
apiInit(catalog, prefix)
}
// Handle uncaught errors in API request code.
// Must be called after apiInit: https://stackoverflow.com/a/72680240
app.use(errorHandler)
const initRoot = require('./lib/initRoot.js')
initRoot(app, argv, localAll, setHeaders, (err) => {
if (err) {
log.error(err, true)
}
// Start server
const initServer = require('./lib/initServer.js')
initServer(catalogs, prefixes, app, argv)
})
}
function apiInit (CATALOG, PREFIX) {
PREFIX = '/' + PREFIX
const PREFIXe = encodeURIComponent(PREFIX).replace('%2F', '/')
const capabilities = metadata(CATALOG, 'capabilities')
const hapiVersion = capabilities.HAPI
log.info('Initializing endpoints for http://localhost:' + argv.port + PREFIX + '/hapi')
// Serve static files in ./public/data (no directory listing provided)
app.use(PREFIXe + '/data', express.static(path.join(__dirname, 'public', 'data')))
app.get(PREFIXe + '/$', function (req, res) {
res.header('Location', 'hapi')
res.status(302).send('')
})
app.get(PREFIXe + '$', function (req, res) {
res.header('Location', PREFIXe + '/hapi')
res.status(302).send('')
})
app.get(PREFIXe + '/hapi/$', function (req, res) {
res.header('Location', '../hapi')
// A 301 is more appropriate, but a relative URL is not allowed.
// Would need to obtain proxied URL in order to get the correct
// absolute URL.
res.status(302).send('')
})
// /hapi
app.get(PREFIX + '/hapi', function (req, res) {
res.on('finish', () => log.request(req))
const landingHTML = metadata(CATALOG, 'server', 'landingHTML')
const landingPath = metadata(CATALOG, 'server', 'landingPath')
setHeaders(res, true)
if (landingHTML !== '') {
res.contentType('text/html')
res.send(landingHTML)
return
}
if (landingPath !== '') {
app.use(PREFIX + '/hapi', express.static(landingPath))
return
}
if (landingPath === '' && landingHTML === '') {
// This will only occur if force=true; otherwise server will not start.
res.status(404).send('Not found.')
}
})
// /about
app.get(PREFIXe + '/hapi/about', function (req, res) {
res.on('finish', () => log.request(req))
setHeaders(res, true)
res.contentType('application/json')
// Send error if query parameters given
if (Object.keys(req.query).length > 0) {
error(req, res, hapiVersion, 1401, 'This endpoint takes no query string.')
return
}
const about = metadata(CATALOG, 'about')
if (about !== undefined) {
res.send(metadata(CATALOG, 'about'))
} else {
res.sendStatus(404)
}
})
// /capabilities
app.get(PREFIXe + '/hapi/capabilities', function (req, res) {
res.on('finish', () => log.request(req))
setHeaders(res, true)
res.contentType('application/json')
// Send error if query parameters given
if (Object.keys(req.query).length > 0) {
error(req, res, hapiVersion, 1401, 'This endpoint takes no query string.')
return
}
res.send(metadata(CATALOG, 'capabilities'))
})
// /catalog
app.get(PREFIXe + '/hapi/catalog', function (req, res) {
res.on('finish', () => log.request(req))
setHeaders(res, true)
res.contentType('application/json')
if (semver.satisfies(hapiVersion + '.0', '>=3.2.0')) {
if (Object.keys(req.query).length === 1) {
if (req.query.depth) {
if (!['all', 'dataset'].includes(req.query.depth)) {
error(req, res, hapiVersion, 1412, "depth must be 'dataset' or 'all'")
return
}
if (req.query.depth === 'all') {
const json = metadata(CATALOG, 'info')
const firstDataset = Object.keys(json)[0]
const all = {
HAPI: json[firstDataset].HAPI,
status: json[firstDataset].status,
catalog: []
}
for (const dataset in json) {
const infoObj = JSON.parse(JSON.stringify(json[dataset]))
delete infoObj.HAPI
delete infoObj.status
all.catalog.push({ id: dataset, info: infoObj })
}
res.send(JSON.stringify(all, null, 2))
} else {
res.send(JSON.stringify(metadata(CATALOG, 'catalog'), null, 2))
}
} else {
error(req, res, hapiVersion, 1401, "catalog takes one query parameter: 'depth'")
}
} else {
res.send(JSON.stringify(metadata(CATALOG, 'catalog'), null, 2))
}
} else {
// Send error if query parameters given
if (Object.keys(req.query).length > 0) {
error(req, res, hapiVersion, 1401, 'This endpoint takes no query string.')
return
}
res.send(JSON.stringify(metadata(CATALOG, 'catalog'), null, 2))
}
})
// /info
app.get(PREFIXe + '/hapi/info', function (req, res) {
res.on('finish', () => log.request(req))
setHeaders(res, true)
res.contentType('application/json')
// Check query string and set defaults as needed.
if (!queryCheck(req, res, hapiVersion, CATALOG, 'info')) {
return // Error already sent, so return;
}
// Get subsetted info response based on requested parameters.
// info() returns integer error code if error.
// TODO: Reconsider this interface to info() -
// infoCheck() is more consistent with other code.
const header = info(req, res, CATALOG)
if (typeof (header) === 'number') {
error(req, res, hapiVersion, 1407)
} else {
res.send(JSON.stringify(header, null, 2))
}
})
// /data
app.get(PREFIXe + '/hapi/data', function (req, res) {
res.on('finish', () => log.request(req))
setHeaders(res, true)
// Check query string and set defaults as needed.
if (!queryCheck(req, res, hapiVersion, CATALOG, 'data')) {
return // Error already sent, so return;
}
// Get subsetted /info response based on requested parameters.
const header = info(req, res, CATALOG)
if (typeof (header) === 'number') {
// One or more of the requested parameters are invalid.
error(req, res, hapiVersion, 1407)
return
}
// Add non-standard elements in header used later in code.
// TODO: Not tested under https.
const proto = req.connection.encrypted ? 'https' : 'http'
header.status.x_request = proto + '://' + req.headers.host + req.originalUrl
header.status.x_startDateRequested = req.query['time.min'] || req.query.start
header.status.x_stopDateRequested = req.query['time.max'] || req.query.stop
header.status.x_parentDataset = req.query.id || req.query.dataset
// timeCheck() returns integer error code if error or true if no error.
const timeOK = timeCheck(header)
if (timeOK !== true) {
error(req, res, hapiVersion, timeOK)
return
}
header.format = 'csv' // Set default format
if (req.query.format) {
if (!['csv', 'json', 'binary'].includes(req.query.format)) {
error(req, res, hapiVersion, 1409,
"Allowed values of 'format' are csv, json, and binary.")
return
}
// Use requested format.
header.format = req.query.format
}
if (req.query.include) {
if (req.query.include !== 'header') {
error(req, res, hapiVersion, 1410,
"Allowed value of 'include' is 'header'.")
return
}
}
// If include was given, set include = true
const include = req.query.include === 'header'
if (header.format === 'csv') { res.contentType('text/csv') }
if (header.format === 'binary') { res.contentType('application/octet-stream') }
if (header.format === 'json') { res.contentType('application/json') }
const start = req.query['time.min'] || req.query.start
const stop = req.query['time.max'] || req.query.stop
const fname = 'dataset-' +
(req.query.id || req.query.dataset) +
'_parameters-' +
req.query.parameters +
'_start-' +
start +
'_stop-' +
stop +
'.' + header.format
if (req.query.attach === 'false') {
// Allow non-standard "attach" query parameter for debugging.
// Content-Type of 'text' will cause browser to display data
// instead of triggering a download dialog.
res.contentType('text')
} else {
res.setHeader('Content-Disposition', 'attachment;filename=' + encodeURIComponent(fname))
}
// Send the data
data(req, res, CATALOG, header, include)
})
// Anything that does not match
// PREFIX + {/hapi,/hapi/capabilities,/hapi/info,/hapi/data,/hapi/about}
app.get(PREFIXe + '/*', function (req, res) {
res.on('finish', () => log.request(req))
if (req.path.startsWith(PREFIXe + '/hapi/')) {
const base = req.headers['x-forwarded-for'] || req.connection.remoteAddress
const msg = 'Invalid URL. See ' + base + PREFIXe + '/hapi'
error(req, res, hapiVersion, 1400, msg, req.originalUrl)
} else {
res.status(404).send('Not found')
}
})
log.info('Initialized endpoints for http://localhost:' + argv.port + PREFIX + '/hapi')
}
function setHeaders (res, cors) {
const packageJSON = require('./package.json')
if (cors) {
// CORS headers
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET')
res.header('Access-Control-Allow-Headers', 'Content-Type')
}
res.header('X-Powered-By', packageJSON.repository.url + ' v' + packageJSON.version)
}
function info (req, res, catalog) {
// Read parameter metadata.
let infoType = 'info'
if (req.query.resolve_references === 'false') {
infoType = 'info-raw'
}
let json = metadata(catalog, infoType, req.query.id || req.query.dataset)
// Copy string metadata (b/c json will be modified).
json = JSON.parse(JSON.stringify(json))
// Create array of known parameter names
const knownparams = []
for (let i = 0; i < json.parameters.length; i++) {
knownparams[i] = json.parameters[i].name
}
// Create array from comma-separated parameters in query
let wantedparams = []
if (req.query.parameters) {
wantedparams = req.query.parameters.split(',')
} else {
// If parameters field not in query string, default is all.
wantedparams = knownparams
}
// Remove repeated parameters from query
wantedparams = Array.from(new Set(wantedparams))
// Catches case where parameters= is given in query string.
// Assume it means same as if no parameters field was given
// (which means all parameters wanted).
if (wantedparams.length === 0) { return json }
// Determine if any parameters requested are invalid
const validparams = []; let iv = 0
const invalidparams = []; let ii = 0
for (let i = 0; i < wantedparams.length; i++) {
if (knownparams.includes(wantedparams[i])) {
// TODO: Consider using objects if parameter lists are very long.
validparams[iv] = wantedparams[i]
iv++
} else {
invalidparams[ii] = wantedparams[i]
ii++
}
}
// Invalid parameter found
if (validparams.length !== wantedparams.length) {
return 1401
}
// Delete parameters from JSON response that were not requested
for (let i = 1; i < knownparams.length; i++) {
if (!wantedparams.includes(knownparams[i])) {
delete json.parameters[i]
}
}
// Remove nulls placed when array element is deleted
json.parameters = json.parameters.filter(function (n) { return n !== undefined })
// Return JSON object
return json
}
function data (req, res, catalog, header, include) {
// Extract command line command and replace placeholders.
const dataConfig = metadata(catalog, 'data')
if (dataConfig.proxy) {
// Not used
const url = replacevars(dataConfig.proxy).replace(/\/$/, '') + req.originalUrl
log.info('Responding with proxy of ' + url)
const httpProxy = require('http-proxy')
const proxy = httpProxy.createProxyServer({})
proxy
.on('end', function (res, socket, head) {
log.info('Responded with proxy of ' + url)
})
.on('error', function (err) {
error(req, res, header.HAPI, 1500, null,
'Proxy ' + url + ' error: ' + err)
})
.web(req, res, { target: dataConfig.proxy })
} else {
const com = buildcom(dataConfig, header, req)
executeCommand(com, req, res, dataConfig.timeout, dataConfig.formats, header, include, (err) => {
if (!err) {
return
}
let code = 1500
let msg = 'Problem with the data server.'
if (typeof (err) === 'string') {
code = parseInt(err.split(',')[0])
msg = err.split(',')[1] || ''
}
if (dataConfig.contact) {
msg += ' Please send URL to ' + dataConfig.contact + '.'
error(req, res, header.HAPI, code, msg.trim())
} else {
error(req, res, header.HAPI, code, 'Problem with the data server.')
}
})
}
function buildcom (d, header, req) {
const start = normalizeTime(req.query['time.min'] || req.query.start)
const stop = normalizeTime(req.query['time.max'] || req.query.stop)
let com = ''
if (d.file || d.url) {
// Will always need to subset in this case
// (unless request is for all variables over
// full range of response, which is not addressed)
// com = config.PYTHONEXE + " " + __dirname + "/lib/subset.py";
com = '"' + config.NODEJSEXE + '" ' + __dirname + '/lib/subset.js'
if (d.file) com = com + ' --file "' + replacevars(d.file) + '"'
if (d.url) com = com + ' --url "' + replacevars(d.url, false) + '"'
com = com + ' --start ' + start
com = com + ' --stop ' + stop
const columns = columnsstr()
if (columns !== '' && d.file && !/\$\{{1,2}parameters\}{1,2}/.test(d.file)) {
com = com + ' --columns ' + columns
}
if (columns !== '' && d.url && !/\$\{{1,2}parameters\}{1,2}/.test(d.url)) {
com = com + ' --columns ' + columns
}
// com = com + " --format " + header["format"];
}
if (d.command) {
com = replacevars(d.command)
const columns = columnsstr()
let subsetcols = false
if (columns !== '') {
subsetcols = true
}
let subsettime = false
if (!/\$\{{1,2}start\}{1,2}/.test(d.command) && !/\$\{{1,2}stop\}{1,2}/.test(d.command)) {
subsettime = true
}
if (subsetcols || subsettime) {
com = com +
' | "' +
config.NODEJSEXE +
'" "' + __dirname +
'/lib/subset.js"'
if (subsettime) {
com = com + ' --start ' + start
com = com + ' --stop ' + stop
}
if (subsetcols) {
com = com + ' --columns ' + columns
}
}
}
if (process.platform.startsWith('win')) {
if (com.startsWith('"')) {
com = '"' + com + '"'
}
} else {
// com = "cpulimit -l 30 -q -- nice -n -20 " + com;
}
return com
}
function columnsstr () {
let fieldstr = ''
if (req.query.parameters && !/\$\{{1,2}parameters\}{1,2}/.test(dataConfig.command)) {
// If command does not contain ${parameters} or ${{parameters}}, assume CL program
// always outputs all variables. Subset the output using subset.js.
const params = req.query.parameters.split(',')
const headerfull = metadata(catalog, 'info', req.query.id || req.query.dataset)
if (params[0] !== headerfull.parameters[0].name) {
// If time variable was not requested, add it
// so first column is always output.
params.unshift(headerfull.parameters[0].name)
}
const fields = {}
let col = 1
let df = 0
// Generate comma separated list of columns to output, e.g.,
// 1,2-4,7
for (let i = 0; i < headerfull.parameters.length; i++) {
df = prod(headerfull.parameters[i].size || [1])
if (df > 1) {
fields[headerfull.parameters[i].name] = col + '-' + (col + df - 1)
} else {
fields[headerfull.parameters[i].name] = col
}
col = col + df
}
for (let i = 0; i < params.length; i++) {
fieldstr = fieldstr + fields[params[i]] + ','
}
fieldstr = fieldstr.slice(0, -1) // Remove last comma.
}
return fieldstr
}
function normalizeTime (timestr) {
// Convert to YYYY-MM-DDTHH:MM:SS.FFFFFFFFFZ
// (nanosecond precision). All command line programs
// will be given this format.
// Need to extract it here and then insert at end
// because moment.utc(timestr).toISOString()
// ignores sub-millisecond parts of time string.
const re = /.*\.[0-9]{3}([0-9].*)Z/
let submilli = '000000'
if (re.test(timestr)) {
submilli = timestr.replace(/.*\.[0-9]{3}([0-9].*)Z$/, '$1')
const pad = '0'.repeat(6 - submilli.length)
submilli = submilli + pad
}
if (/^[0-9]{4}Z$/.test(timestr)) {
timestr = timestr.slice(0, -1) + '-01-01T00:00:00.000Z'
}
if (/^[0-9]{4}-[0-9]{2}Z$/.test(timestr)) {
timestr = timestr.slice(0, -1) + '-01T00:00:00.000Z'
}
if (/^[0-9]{4}-[0-9]{3}Z$/.test(timestr)) {
timestr = timestr.slice(0, -1) + 'T00:00:00.000Z'
}
if (/^[0-9]{4}-[0-9]{2}-[0-9]{2}Z$/.test(timestr)) {
timestr = timestr.slice(0, -1) + 'T00:00:00.000Z'
}
timestr = moment.utc(timestr).toISOString()
timestr = timestr.slice(0, -1) + submilli + 'Z'
return timestr
}
function replacevars (com, isURL) {
const dataset = req.query.id || req.query.dataset
// Double {{ }} means don't quote
com = com.replace('${{id}}', dataset)
com = com.replace('${{dataset}}', dataset)
if (req.query.parameters) {
com = com.replace('${{parameters}}', req.query.parameters)
} else {
com = com.replace('${{parameters}}', '')
}
const start = req.query['time.min'] || req.query.start
const stop = req.query['time.max'] || req.query.stop
// Times don't need to be quoted
com = com.replace('${start}', normalizeTime(start))
com = com.replace('${stop}', normalizeTime(stop))
com = com.replace('${{start}}', normalizeTime(start))
com = com.replace('${{stop}}', normalizeTime(stop))
if (!isURL) {
if (process.platform.startsWith('win')) {
com = com.replace('${id}', '"' + dataset + '"')
com = com.replace('${dataset}', '"' + dataset + '"')
} else {
com = com.replace('${id}', "'" + dataset + "'")
com = com.replace('${dataset}', '"' + dataset + '"')
}
}
if (req.query.parameters) {
if (process.platform.startsWith('win')) {
if (isURL) {
com = com.replace('${parameters}', '"' + req.query.parameters + '"')
} else {
com = com.replace('${parameters}', req.query.parameters)
}
} else {
if (isURL === true) {
com = com.replace('${parameters}', req.query.parameters)
} else {
com = com.replace('${parameters}', "'" + req.query.parameters + "'")
}
}
} else {
com = com.replace('${parameters}', '""')
}
com = com.replace('${format}', header.format)
return com
}
}
function prod (arr) {
// Multiply all elements in array
return arr.reduce(function (a, b) { return a * b })
}
function queryCheck (req, res, hapiVersion, catalog, type) {
// Check query parameters for query of type='info' or 'data'
// If invalid query parameter, send error and return false
// If req.query.resolve_references === undefined, set to "true"
const catalogObj = metadata(catalog, 'catalog')
// Check for required id or dataset parameter
let dataset = req.query.id
let emsg = 'A dataset id must be given.'
if (parseInt(catalogObj.HAPI) >= 3) {
dataset = dataset || req.query.dataset
emsg = 'A dataset must be given.'
}
if (!dataset) {
error(req, res, hapiVersion, 1400, emsg)
return false
}
if (req.query.id && req.query.dataset) {
error(req, res, hapiVersion, 1400, "Only one of 'id' and 'dataset' is allowed.")
return false
}
const ids = metadata(catalog, 'ids')
if (!ids.includes(dataset)) {
error(req, res, hapiVersion, 1406)
return false
}
if (type === 'info') {
// Check if extra parameters given
const allowed = ['id', 'parameters', 'resolve_references']
let emsg = "'id', 'parameters', and 'resolve_references' are the only valid query parameters."
if (parseInt(catalogObj.HAPI) >= 3) {
allowed.push('dataset')
emsg = "'dataset' or 'id', 'parameters', and 'resolve_references' are the only valid query parameters."
}
for (const key in req.query) {
if (!allowed.includes(key)) {
error(req, res, hapiVersion, 1401, emsg)
return false
}
}
} else {
// Check if query parameters are all valid
const allowed = [
'id', 'parameters', 'time.min', 'time.max',
'format', 'include', 'attach', 'resolve_references'
]
if (parseInt(catalogObj.HAPI) >= 3) {
allowed.push('dataset')
allowed.push('start')
allowed.push('stop')
}
for (const key in req.query) {
if (!allowed.includes(key)) {
error(req, res, hapiVersion, 1401,
'The only allowed query parameters are ' + allowed.join(', '))
return false
}
}
if (!req.query['time.min'] && !req.query.start) {
error(req, res, hapiVersion, 1402)
return
}
if (!req.query['time.max'] && !req.query.start) {
error(req, res, hapiVersion, 1403)
return
}
}
// Set default resolve_references to be true if not given
if (req.query.resolve_references === undefined) {
req.query.resolve_references = 'true'
}
// If resolve_references given, check that it is "true" or "false".
if (!['true', 'false'].includes(req.query.resolve_references)) {
error(req, res, hapiVersion, 1411, "resolve_references must be 'true' or 'false'")
return false
}
return true
}
function timeCheck (header) {
const validHAPITime = require('hapi-server-verifier').is.HAPITime
// TODO: Handle less than milliseconds resolution.
// TODO: If one of the times had Z and the other does not,
// should warn that all time stamps are interpreted as Z.
const times = [
header.status.x_startDateRequested,
header.status.x_stopDateRequested,
header.startDate, header.stopDate
]
for (let i = 0; i < times.length; i++) {
// moment.js says YYYY-MM-DD and YYYY-DOY with no Z is
// "... not in a recognized RFC2822 or ISO format. moment
// construction falls back to js Date(), which is not reliable
// across all browsers and versions."
// But HAPI says it is valid.
times[i] = times[i].replace(/Z$/, '')
if (times[i].length === 8 || times[i].length === 10) {
times[i] = times[i] + 'T00:00:00.000'
}
// YYYY or YYYYZ is not valid ISO according to moment.js
if (times[i].length === 4) {
times[i] = times[i] + '-01-01T00:00:00.000'
}
// Make all times UTC
times[i] = times[i] + 'Z'
times[i] = times[i].replace(/\.Z/, '.0Z') // moment.js says .Z is invalid.
}
let r = validHAPITime(times[0], header.HAPI)
if (r.error) {
return 1402
}
r = validHAPITime(times[1], header.HAPI)
if (r.error) {
return 1403
}
function leapshift (time) {
let shift = 0
if (time.match(/^[0-9]{4}-[0-9]{3}/)) {
if (time.match(/23:59:60/)) {
time = time.replace(/:60/, ':59')
shift = 1000
}
}
if (time.match(/^[0-9]{4}-[0-9]{2}-/)) {
if (time.match(/23:59:60/)) {
time = time.replace(/:60/, ':59')
shift = 1000
}
}
return { time, shift }
}
const timesms = []
for (let i = 0; i < 4; i++) {
let shift = 0
if (!moment(times[0], moment.ISO_8601).isValid()) {
// Change 60th second to 59.
const obj = leapshift(times[0])
if (obj.shift > 0) { // Error was due to leap second.
times[0] = obj.time
shift = obj.shift
} else {
return 1500 // Unexpected error.
}
}
timesms[i] = moment(times[i]).valueOf() + shift
}
if (timesms[1] <= timesms[0]) { // Stop requested <= start requested
return 1404
}
if (timesms[0] < timesms[2]) { // Start requested < start available
return 1405
}
if (timesms[1] > timesms[3]) { // Stop requested > stop available
return 1405
}
return true
}
// HAPI errors
function error (req, res, hapiVersion, code, message, messageFull) {
let start = 'time.min'
let stop = 'time.max'
let dataset = 'id'
if (parseInt(hapiVersion) >= 3) {
start = req.query.start ? 'start' : start
stop = req.query.stop ? 'stop' : stop
dataset = req.query.dataset ? 'dataset' : dataset
}
const errs = {
1400: { status: 400, message: 'HAPI error 1400: user input error' },
1401: { status: 400, message: 'HAPI error 1401: unknown request field' },
1402: { status: 400, message: 'HAPI error 1402: error in ' + start },
1403: { status: 400, message: 'HAPI error 1403: error in ' + stop },
1404: { status: 400, message: 'HAPI error 1404: ' + start + ' equal to or after ' + stop },
1405: { status: 400, message: 'HAPI error 1405: time outside valid range' },
1406: { status: 404, message: 'HAPI error 1406: unknown ' + dataset },
1407: { status: 404, message: 'HAPI error 1407: unknown dataset parameter' },
1408: { status: 400, message: 'HAPI error 1408: too much time or data requested' },
1409: { status: 400, message: 'HAPI error 1409: unsupported output format' },
1410: { status: 400, message: 'HAPI error 1410: unsupported include value' },
1411: { status: 400, message: 'HAPI error 1411: unsupported resolve_references value' },
1412: { status: 400, message: 'HAPI error 1412: unsupported depth value' },
1500: { status: 500, message: 'HAPI error 1500: internal server error' },
1501: { status: 500, message: 'HAPI error 1501: upstream request error' }
}
// Defaults
const json = {
HAPI: hapiVersion,
status: { code: 1500, message: 'Internal server error' }
}
let httpcode = 500
let httpmesg = 'Internal server error. Please report URL attempted to the ' +
" <a href='https://github.com/hapi-server/server-nodejs/issues'>issue tracker</a>."
// Modify defaults
if (errs[code + '']) {
json.status.code = code
json.status.message = errs[code + ''].message
httpcode = errs[code + ''].status
httpmesg = errs[code + ''].message
}
if (message) {
json.status.message = json.status.message.replace(/\:.*/, ': ' + message)
}
const ecode = httpcode + '/' + json.status.code
messageFull = messageFull || message
if ((code + '').startsWith('15')) {
log.error('HTTP/HAPI error ' + ecode + '; ' + messageFull)
}
res.on('finish', () => log.request(req))
if (res.headersSent) {
log.error('HTTP headers were already sent. Not setting status code, but appending JSON error message.')
res.write(JSON.stringify(json, null, 4) + '\n')
res.end()
} else {
res.contentType('application/json')
res.statusMessage = httpmesg
log.debug('Sending JSON error message.')
res.status(httpcode).send(JSON.stringify(json, null, 4) + '\n')
}
const addr = req.headers['x-forwarded-for'] || req.connection.remoteAddress
const msg = 'Responded with ' + ecode + ' to ' + addr + ': ' + req.originalUrl
log.info(msg)
}
// Uncaught errors in API request code.
function errorHandler (err, req, res, next) {
const addr = req.headers['x-forwarded-for'] || req.connection.remoteAddress
const msg = 'Request from ' + addr + ': ' + req.originalUrl + '\n' + err.stack
error(req, res, '', '1500', null, msg)
}
// Errors in non-API part of application
function exceptions () {
process.on('uncaughtException', function (err) {
if (err.errno === 'EADDRINUSE') {
log.error('Port ' + argv.port + ' already in use.', 1)
} else {
console.log(err)
console.log(argv.test)
log.error('Uncaught Exception\n' + err, argv.test === undefined ? 0 : 1)
}
})
}