Mercurial > urweb
diff src/c/urweb.c @ 1728:95d3b4f26f59
Ensure proper ordering of <script> execution, to bring identifiers into scope in time
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Fri, 27 Apr 2012 09:43:09 -0400 |
parents | 4df4521fbd3b |
children | 6817ddd6cf1f |
line wrap: on
line diff
--- a/src/c/urweb.c Fri Apr 27 07:35:59 2012 -0400 +++ b/src/c/urweb.c Fri Apr 27 09:43:09 2012 -0400 @@ -1312,10 +1312,6 @@ ctx->script.front += len; } -const char *uw_Basis_get_script(uw_context ctx, uw_unit u) { - return "<sc>"; -} - const char *uw_get_real_script(uw_context ctx) { if (strstr(ctx->outHeaders.start, "Set-Cookie: ")) { uw_write_script(ctx, "sig=\""); @@ -3157,6 +3153,8 @@ return ctx->app ? ctx->app->db_rollback(ctx) : 0; } +static const char begin_xhtml[] = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"; + void uw_commit(uw_context ctx) { int i; @@ -3210,36 +3208,53 @@ uw_check(ctx, 1); *ctx->page.front = 0; - // Splice script data into appropriate part of page - if (ctx->returning_indirectly || ctx->script_header[0] == 0) { - char *start = strstr(ctx->page.start, "<sc>"); - if (start) { - memmove(start, start + 4, uw_buffer_used(&ctx->page) - (start - ctx->page.start) - 4); - ctx->page.front -= 4; - } - } else if (uw_buffer_used(&ctx->script) == 0) { - size_t len = strlen(ctx->script_header); - char *start = strstr(ctx->page.start, "<sc>"); - if (start) { - ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) - 4 + len); - start = strstr(ctx->page.start, "<sc>"); - memmove(start + len, start + 4, uw_buffer_used(&ctx->page) - (start - ctx->page.start) - 3); - ctx->page.front += len - 4; - memcpy(start, ctx->script_header, len); - } - } else { - size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script); - size_t lenP = lenH + 40 + len; - char *start = strstr(ctx->page.start, "<sc>"); - if (start) { - ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) - 4 + lenP); - start = strstr(ctx->page.start, "<sc>"); - memmove(start + lenP, start + 4, uw_buffer_used(&ctx->page) - (start - ctx->page.start) - 3); - ctx->page.front += lenP - 4; + if (!ctx->returning_indirectly && !strncmp(ctx->page.start, begin_xhtml, sizeof begin_xhtml - 1)) { + char *s; + + // Splice script data into appropriate part of page, also adding <head> if needed. + s = ctx->page.start + sizeof begin_xhtml - 1; + s = strchr(s, '<'); + if (s == NULL) { + // Weird. Document has no tags! + + uw_write(ctx, "<head></head><body></body>"); + uw_check(ctx, 1); + *ctx->page.front = 0; + } else if (!strncmp(s, "<head>", 6)) { + // <head> is present. Let's add the <script> tags immediately after it. + + size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script); + size_t lenP = lenH + 40 + len; + char *start = s + 6, *oldPage = ctx->page.start; + + ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP); + start += ctx->page.start - oldPage; + memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1); + ctx->page.front += lenP; memcpy(start, ctx->script_header, lenH); memcpy(start + lenH, "<script type=\"text/javascript\">", 31); memcpy(start + lenH + 31, ctx->script.start, len); memcpy(start + lenH + 31 + len, "</script>", 9); + } else { + // No <head>. At this point, add it, with <script> tags inside. + + size_t lenH = strlen(ctx->script_header), len = uw_buffer_used(&ctx->script); + size_t lenP = lenH + 53 + len; + char *start = s, *oldPage = ctx->page.start; + + printf("start = %ld\n", start - ctx->page.start); + + ctx_uw_buffer_check(ctx, "page", &ctx->page, uw_buffer_used(&ctx->page) + lenP); + start += ctx->page.start - oldPage; + printf("page1 = %s\n", ctx->page.start); + memmove(start + lenP, start, uw_buffer_used(&ctx->page) - (start - ctx->page.start) + 1); + printf("page2 = %s\n", ctx->page.start); + ctx->page.front += lenP; + memcpy(start, "<head>", 6); + memcpy(start + 6, ctx->script_header, lenH); + memcpy(start + 6 + lenH, "<script type=\"text/javascript\">", 31); + memcpy(start + 6 + lenH + 31, ctx->script.start, len); + memcpy(start + 6 + lenH + 31 + len, "</script></head>", 16); } } } @@ -3919,8 +3934,6 @@ return NULL; } -static const char begin_xhtml[] = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"; - failure_kind uw_begin_onError(uw_context ctx, char *msg) { int r = setjmp(ctx->jmp_buf);