Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/responses/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { DEFAULT_UNAUTHORIZED_HTML_MESSAGE } from '../utils/constants.js';

export function daResp({
body, status, contentType, contentLength,
}) {
Expand All @@ -27,8 +29,12 @@ export function daResp({
return new Response(body, { status, headers });
}

export function get404() {
return daResp({ body: '', status: 404 });
export function get404(message = '') {
return daResp({ body: message, status: 404, contentType: 'text/html' });
}

export function get401(message = DEFAULT_UNAUTHORIZED_HTML_MESSAGE) {
return daResp({ body: message, status: 404, contentType: 'text/html' });
}

export function getRobots() {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { daResp } from '../responses/index.js';
import { daResp, get401 } from '../responses/index.js';
import { DEFAULT_CORS_HEADERS, TRUSTED_ORIGINS } from '../utils/constants.js';

export function getCookie({ req }) {
Expand All @@ -33,5 +33,5 @@ export function getCookie({ req }) {
return new Response('cookie set', { headers: respHeaders });
}
}
return daResp({ body: '401 Unauthorized', status: 401, contentType: 'text/plain' });
return get401();
}
28 changes: 12 additions & 16 deletions src/routes/da-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import putHelper from '../helpers/source.js';
import { removeUEAttributes, unwrapParagraphs } from '../ue/attributes.js';
import { prepareHtml } from '../ue/ue.js';
import { getAemCtx, getAEMHtml } from '../utils/aemCtx.js';
import { daResp } from '../responses/index.js';
import { daResp, get401, get404 } from '../responses/index.js';
import { BRANCH_NOT_FOUND_HTML_MESSAGE, DEFAULT_HTML_TEMPLATE, UNAUTHORIZED_HTML_MESSAGE } from '../utils/constants.js';
import { getSiteConfig } from '../storage/config.js';

Expand Down Expand Up @@ -67,25 +67,16 @@ export async function daSourceGet({ req, env, daCtx }) {
org, site, path, ext, authToken,
} = daCtx;

const response = {
status: 200,
contentType: 'text/html; charset=utf-8',
};

// check if Authorization header is present
if (!authToken) {
response.body = UNAUTHORIZED_HTML_MESSAGE;
response.status = 401;
return daResp(response);
return get401(UNAUTHORIZED_HTML_MESSAGE);
}

// get the AEM parts (head.html)
const aemCtx = getAemCtx(env, daCtx);
const headHtml = await getAEMHtml(aemCtx, '/head.html');
if (!headHtml) {
response.body = BRANCH_NOT_FOUND_HTML_MESSAGE;
response.status = 404;
return daResp(response);
return get404(BRANCH_NOT_FOUND_HTML_MESSAGE);
}

// get the content from DA admin
Expand All @@ -102,21 +93,26 @@ export async function daSourceGet({ req, env, daCtx }) {
method: 'GET',
headers,
});
let body;
const daAdminResp = await env.daadmin.fetch(req);
if (daAdminResp && daAdminResp.status === 200) {
// enrich stored content with HTML header and UE attributes
const originalBodyHtml = await daAdminResp.text();
const responseHtml = await prepareHtml(daCtx, aemCtx, originalBodyHtml, headHtml);
response.body = responseHtml;
body = responseHtml;
} else {
// enrich default template with HTML header and UE attributes
const templateHtml = await getPageTemplate(env, daCtx, aemCtx, headHtml);
const responseHtml = await prepareHtml(daCtx, aemCtx, templateHtml, headHtml);
response.body = responseHtml;
body = responseHtml;
}

response.contentLength = response.body.length;
return daResp(response);
return daResp({
status: 200,
body,
contentLength: body.length,
contentType: 'text/html; charset=utf-8',
});
}

export async function daSourcePost({ req, env, daCtx }) {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ export const UNAUTHORIZED_HTML_MESSAGE = `
export const DEFAULT_HTML_TEMPLATE = '<body><header></header><main><div></div></main><footer></footer></body>';

export const BRANCH_NOT_FOUND_HTML_MESSAGE = '<html><body><h1>Not found: Unable to retrieve AEM branch</h1></body></html>';

export const DEFAULT_UNAUTHORIZED_HTML_MESSAGE = '<html><body><h1>401: Unauthorized</h1></body></html>';