2022-01-07 22:23:47 +01:00

91 lines
2.1 KiB
JavaScript

// Uplink Gemini Server
// Request Class
const url = require('url').URL;
module.exports = class Request {
constructor() {
this.rawRequest = '';
this.protocol = 'gemini:';
this.hostname = 'localhost';
this.port = 1965;
this.path = '/';
this.search = '';
this.clientCert = undefined;
this.content = '';
this.contentMime = '';
this.clientIP = '';
this.clientPort = '';
this.clientFamily = '';
}
parseRequest() {
var tempRequest = this.rawRequest.toString();
var tempUrl = tempRequest.substring(0, tempRequest.indexOf('\r\n'));
if(tempUrl.startsWith('titan:')) {
var urlComponents = tempUrl.split(';');
tempUrl = urlComponents[0];
for(var i = 1; i < urlComponents.length; i++) {
if(urlComponents[i].startsWith('mime=')) {
this.contentMime = urlComponents[i].replace('mime=', '');
} else if (urlComponents[i].startsWith('size=')) {
this.contentSize = urlComponents[i].replace('size=', '');
} else if (urlComponents[i].startsWith('token=')) {
this.contentToken = urlComponents[i].replace('token=', '');
}
}
}
//var lines = tempUrl.substring(0,)
tempUrl = tempUrl.trim();
try {
const geminiUrl = new url(tempUrl);
this.protocol = geminiUrl.protocol;
this.hostname = geminiUrl.hostname;
if (geminiUrl.port) this.port = geminiUrl.port;
this.path = geminiUrl.pathname;
this.search = geminiUrl.search.substring(1); //remove '?' from string
if(this.protocol === 'titan:') {
this.content = this.rawRequest.slice(this.rawRequest.indexOf('\r\n') + 3);
}
} catch(err) {
console.log(err);
}
}
setRawRequest(rawRequest) {
this.rawRequest = rawRequest;
}
getRawRequest() {
return this.rawRequest;
}
setClientCert(clientCert) {
this.clientCert = clientCert;
}
setClientIP(ipAddress) {
this.clientIP = ipAddress;
}
getClientIP() {
return this.clientIP;
}
setClientPort(port) {
this.clientPort = port;
}
getClientPort() {
return this.clientPort;
}
setClientFamily(family) {
this.clientFamily = family;
}
getClientFamily() {
return this.clientFamily;
}
};