Home > Archive > PERL CGI Beginners > May 2004 > Apache error 500: malformed header from script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Apache error 500: malformed header from script
|
|
| Jan Eden 2004-05-22, 11:32 am |
| Hi all,
I initially posted this to the perl@lists.mysql.com mailing list, but it se=
ems to be more of a CGI problem. This is the situation:
1. My script (edit.pl) reads a row of data from a database and displays it =
in an html form for editing.
2. On submitting, the same script is called (in 'commit' mode) to write the=
updated data to the database.
3. After the update, another script (show.pl) is called to load the updated=
data and print it to the screen with my site's layout.
Steps 1 and 2 always (!) work fine, even if the error occurrs.
In most cases, step 3 also works fine. But if the content loaded from the d=
atabase is quite long (or is not properly encoded in utf8), I get the "malf=
ormed header from script" error.
Now there"s a funny thing: If I do not load the content itself (via a varia=
ble $content) from the database into the form field, but just print out the=
word "content" and paste the original content into the field, I never get =
the error.
That's strange, since the content transferred as a CGI parameter should be =
the same in both cases: The original content. The only difference is the st=
ring printed to the form field when the form is generated.
I experimented a bit. Simple strings (like "content") never cause an error.=
But if I add some tab stops and newlines, the error returns.
So I have been able to track down the source of the problem, but I just can=
not understand it.
Could anybody help me here? Below you find some code from my script's code.=
I know it's less elegant than it could be and I already got some hints for=
fine tuning it, but I'd like to solve the serious problem first.
Thanks,
Jan
----
#!/usr/bin/perl -w
use strict;
use DBI;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
# mein Modul f=FCr user_check, mysql_config und headfoot
use Site2;
my $q =3D new CGI;
# Standardmodus ist lokal
my $mode =3D $q->param('mode') || 'local';
# Konfiguration fuer die Verbindung zum MySQL-Server
my ($server, $db, $username, $password) =3D mysql_config($mode);
my $dbh =3D DBI->connect("dbi:mysql:$db:$server", $username, $password, { =
RaiseError =3D> 1 });
# checken, ob es sich um Edition oder Neueintrag handelt
my $id =3D $q->param('id') || '';
# Einlesen des eingegebenen Benutzernamen und Passworts=20
my $user_name_input =3D $q->param('user_name') || '';
my $user_password_input =3D $q->param('user_password') || '';
# legt fest, ob eine Maske ausgegeben oder die Daten uebertragen werden
my $edit_mode =3D $q->param('edit_mode') || '';
# legt die Mutter des neuen Eintrags fest (wird von show.pl =FCbergeben)
my $mother_id =3D $q->param('mother_id');
# Abrufen des Titels der Mutterseite f=FCr das Men=FC
my $query =3D "SELECT title FROM pages WHERE page_id =3D $mother_id";=20
my $sth =3D $dbh->prepare($query);
$sth->execute();
my ($mother_title) =3D $sth->fetchrow_array;
$sth->finish();
# Bestimmte Muttertitel werden nicht gedruckt (Home, Public, Private)
$mother_title =3D $mother_id =3D=3D 1 || $mother_id =3D=3D 2 || $mother_id =
=3D=3D 2200 ? '' : '|' . $mother_title . '|';
my $title =3D 'Editieren und Einfügen';
# Einlesen des Seitenkopfes und -fu=DFes (in Abh=E4ngigkeit von verschieden=
en Variablen)
my ($page_head, $page_foot) =3D headfoot($title, $mother_id, $mother_title,=
$mode);
# Deklaration verschiedener Variablen (leer f=FCr Neueintr=E4ge)
my ($author_id, $content, $link, $page_type, $user_id, $pdf, $tex) =3D ();
# Ausdruck des Headers und Seitenkopfes
print $q->header(-type=3D>'text/html', -charset=3D>'utf-8'), $page_head unl=
ess $edit_mode eq 'commit';
# Wenn noch kein Benutzername/Passwort angegeben ist: Identifizierung
# Wenn Identifizierung nur au=DFerhalb des lokalen Modus gew=FCnscht ist: "=
$mode eq 'local' ||" zum Konditional hinzuf=FCgen=20
unless ($mode eq 'local' || $user_name_input && $user_password_input) {
print qq{<h1>Identifizierung</h1><p>Bearbeitungsfunktionen sind passwor=
tgesch=FCtzt. Bitte geben Sie Benutzernamen und Passwort ein.</p><form acti=
on=3D"edit.pl" method=3D"post" enctype=3D"application/x-www-form-urlencoded=
" accept-charset=3D"utf-8"><table><tr><td>Benutzer:</td><td><input type=3D"=
text" name=3D"user_name" value=3D"" size=3D"20" /></td></tr><tr><td>Passwor=
t:</td><td><input type=3D"password" name=3D"user_password" size=3D"20" /></=
td></tr></table><input type=3D"hidden" name=3D"id" value=3D"$id" /><input t=
ype=3D"hidden" name=3D"mother_id" value=3D"$mother_id" /><input type=3D"hid=
den" name=3D"mode" value=3D"$mode" /> <input type=3D"submit" value=3D"Absen=
den" /></form>};
}
# Wenn Benutzername/Passwort vorhanden sind: =FCberpr=FCfen (dito zum lokal=
en Modus)
elsif ($mode eq 'local' || user_check(1, $user_name_input, $user_password_i=
nput, $mode)) {
# Abrufen des Datensatzes, wenn eine $id vorhanden ist
if ($id) {
print qq{<h2>Editieren</h2>};
my $query =3D "SELECT mother_id, author_id, title, content, link, p=
age_type, user_id, pdf, tex FROM pages WHERE page_id =3D $id";
my $sth =3D $dbh->prepare($query);
$sth->execute();
($mother_id, $author_id, $title, $content, $link, $page_type, $user=
_id, $pdf, $tex) =3D $sth->fetchrow_array;
$sth->finish();
}
# sonst: Festlegen der Standardwerte f=FCr den Neueintrag
else {
print qq{<h2>Neueintrag</h2>};
($author_id, $page_type, $user_id, $pdf, $tex) =3D (1,2,0,0,0);
}
######## print out the form here #############
print qq{<form action=3D"edit.pl" method=3D"post" enctype=3D"applicatio=
n/x-www-form-urlencoded" accept-charset=3D"utf-8"><input type=3D"submit" va=
lue=3D"Absenden" /><table><tr><td>Page_ID:</td><td><input type=3D"text" nam=
e=3D"id" value=3D"$id" size=3D"5" readonly /></td></tr><tr><td>Mother_ID:</=
td><td><input type=3D"text" size=3D"5" name=3D"mother_id" value=3D"$mother_=
id" /></td></tr><tr><td>Author_ID:</td><td><input type=3D"text" size=3D"5" =
name=3D"author_id" value=3D"$author_id" /></td></tr><tr><td>User_ID:</td><t=
d><input type=3D"text" size=3D"5" name=3D"user_id" value=3D"$user_id" /></t=
d></tr><tr><td>Page_Type:</td><td><input type=3D"text" size=3D"5" name=3D"p=
age_type" value=3D"$page_type" /></td></tr><tr><td>Link:</td><td><input typ=
e=3D"text" size=3D"120" name=3D"link" value=3D"$link" /></td></tr><tr><td>P=
DF:</td><td><input type=3D"text" size=3D"5" name=3D"pdf" value=3D"$pdf" /><=
/td></tr><tr><td>LaTeX:</td><td><input type=3D"text" size=3D"5" name=3D"tex=
" value=3D"$tex" /></td></tr><tr><td>Page_Title:</td><td><input type=3D"tex=
t" size=3D"120" name=3D"title" value=3D"$title" /></td></tr><tr><td>Content=
:</td><td><textarea name=3D"content" rows=3D"30" cols=3D"120">$content</tex=
tarea></td></tr></table><input type=3D"hidden" name=3D"mode" value=3D"$mode=
" /> <input type=3D"hidden" name=3D"user_name" value=3D"$user_name_input" /=
> <input type=3D"hidden" name=3D"user_password" value=3D"$user_password_inp=
ut" /> <input type=3D"hidden" name=3D"edit_mode" value=3D"commit" /></form>=
}; }
# bei nicht erfolgreicher Identifizierung
else {
print qq{<h1>Falsches Passwort!</h1><p>Bitte verwenden Sie den "Zur=FCc=
k"-Button Ihres Browsers, um die Eingaben zu korrigieren.};
}
print $page_foot unless $edit_mode eq 'commit';
# =DCbertragungsmodus
# f=FCr erneute =DCberpr=FCfung hinzuf=FCgen: "&& user_check(1, $user_name_=
input, $user_password_input, $mode)"
if ($edit_mode eq 'commit') {
# =DCbernahme der Parameter aus der Editierungs- bzw. Neueintragsmaske
my $mother_id =3D $q->param('mother_id');
my $author_id =3D $q->param('author_id');
my $user_id =3D $q->param('user_id');
my $page_type =3D $q->param('page_type');
my $title =3D $dbh->quote($q->param('title'));
my $content =3D $dbh->quote($q->param('content'));
my $link =3D $dbh->quote($q->param('link'));
my $pdf =3D $q->param('pdf');
my $tex =3D $q->param('tex');
# bei vorhandener ID: Update des Datensatzes
if ($id) {
my $query =3D "UPDATE pages SET mother_id =3D $mother_id, author_id=
=3D $author_id, user_id =3D $user_id, page_type =3D $page_type, title =3D =
$title, content =3D $content, link =3D $link, pdf =3D $pdf, tex =3D $tex WH=
ERE page_id =3D $id";
my $sth =3D $dbh->prepare($query);
$sth->execute();
$sth->finish();
print $q->redirect("show.pl?mode=3Dlocal&id=3D$id");
}
# sonst: Einf=FCgen eines neuen Datensatzes
else {
my $query =3D "INSERT INTO pages (mother_id, author_id, user_id, ti=
tle, content, link, page_type, pdf, tex) VALUES ($mother_id, $author_id, $u=
ser_id, $title, $content, $link, $page_type, $pdf, $tex)";
my $sth =3D $dbh->prepare($query);
$sth->execute();
$sth->finish();
print $q->redirect("show.pl?mode=3Dlocal&id=3D$mother_id");
} =20
}
$dbh->disconnect;
--=20
How many Microsoft engineers does it take to screw in a lightbulb? None. Th=
ey just redefine "dark" as the new standard.
| |
| Jan Eden 2004-05-22, 11:32 am |
| I solved the problem and apologize for the inconvenience. I overlooked a ce=
rtain print statement which would be sent to the browser before printing a =
header under certain circumstances.
Thanks - Jan
Jan Eden wrote on 02.05.2004:
>Hi all,
>
>I initially posted this to the perl@lists.mysql.com mailing list,
>but it seems to be more of a CGI problem. This is the situation:
>
>1. My script (edit.pl) reads a row of data from a database and
>displays it in an html form for editing.
>
>2. On submitting, the same script is called (in 'commit' mode) to
>write the updated data to the database.
>
>3. After the update, another script (show.pl) is called to load the
>updated data and print it to the screen with my site's layout.
>
>Steps 1 and 2 always (!) work fine, even if the error occurrs.
>
>In most cases, step 3 also works fine. But if the content loaded
>from the database is quite long (or is not properly encoded in
>utf8), I get the "malformed header from script" error.
>
>Now there"s a funny thing: If I do not load the content itself (via
>a variable $content) from the database into the form field, but just
>print out the word "content" and paste the original content into the
>field, I never get the error.
>
>That's strange, since the content transferred as a CGI parameter
>should be the same in both cases: The original content. The only
>difference is the string printed to the form field when the form is
>generated.
>
>I experimented a bit. Simple strings (like "content") never cause an
>error. But if I add some tab stops and newlines, the error returns.
>
>So I have been able to track down the source of the problem, but I
>just cannot understand it.
>
>Could anybody help me here? Below you find some code from my
>script's code. I know it's less elegant than it could be and I
>already got some hints for fine tuning it, but I'd like to solve the
>serious problem first.
>
>Thanks,
>
>Jan
>
>---- #!/usr/bin/perl -w
>
>use strict; use DBI; use CGI; use CGI::Carp qw(fatalsToBrowser); #
>mein Modul f=FCr user_check, mysql_config und headfoot use Site2;
>
>my $q =3D new CGI;
>
># Standardmodus ist lokal my $mode =3D $q->param('mode') || 'local';
>
># Konfiguration fuer die Verbindung zum MySQL-Server my ($server,
>$db, $username, $password) =3D mysql_config($mode); my $dbh =3D
>DBI->connect("dbi:mysql:$db:$server", $username, $password, {
>RaiseError =3D> 1 });
>
># checken, ob es sich um Edition oder Neueintrag handelt my $id =3D
>$q->param('id') || '';
>
># Einlesen des eingegebenen Benutzernamen und Passworts my
>$user_name_input =3D $q->param('user_name') || ''; my
>$user_password_input =3D $q->param('user_password') || '';
>
># legt fest, ob eine Maske ausgegeben oder die Daten uebertragen
>werden my $edit_mode =3D $q->param('edit_mode') || ''; # legt die
>Mutter des neuen Eintrags fest (wird von show.pl =FCbergeben) my
>$mother_id =3D $q->param('mother_id');
>
># Abrufen des Titels der Mutterseite f=FCr das Men=FC my $query =3D
>"SELECT title FROM pages WHERE page_id =3D $mother_id"; my $sth =3D
>$dbh->prepare($query); $sth->execute(); my ($mother_title) =3D
>$sth->fetchrow_array; $sth->finish();
>
># Bestimmte Muttertitel werden nicht gedruckt (Home, Public,
>Private) $mother_title =3D $mother_id =3D=3D 1 || $mother_id =3D=3D 2 ||
>$mother_id =3D=3D 2200 ? '' : '|' . $mother_title . '|';
>
>my $title =3D 'Editieren und Einfügen';
>
># Einlesen des Seitenkopfes und -fu=DFes (in Abh=E4ngigkeit von
>verschiedenen Variablen) my ($page_head, $page_foot) =3D
>headfoot($title, $mother_id, $mother_title, $mode);
>
># Deklaration verschiedener Variablen (leer f=FCr Neueintr=E4ge) my
>($author_id, $content, $link, $page_type, $user_id, $pdf, $tex) =3D
>();
>
># Ausdruck des Headers und Seitenkopfes print
>$q->header(-type=3D>'text/html', -charset=3D>'utf-8'), $page_head unless
>$edit_mode eq 'commit';
>
># Wenn noch kein Benutzername/Passwort angegeben ist:
>Identifizierung # Wenn Identifizierung nur au=DFerhalb des lokalen
>Modus gew=FCnscht ist: "$mode eq 'local' ||" zum Konditional
>hinzuf=FCgen unless ($mode eq 'local' || $user_name_input &&
>$user_password_input) {
>print qq{<h1>Identifizierung</h1><p>Bearbeitungsfunktionen sind=20
>passwortgesch=FCtzt. Bitte geben Sie Benutzernamen und Passwort
>ein.</p><form action=3D"edit.pl" method=3D"post"
>enctype=3D"application/x-www-form-urlencoded"
>accept-charset=3D"utf-8"><table><tr><td>Benutzer:</td><td><input
>type=3D"text" name=3D"user_name" value=3D"" size=3D"20"
>/></td></tr><tr><td>Passwort:</td><td><input type=3D"password"
>name=3D"user_password" size=3D"20" /></td></tr></table><input
>type=3D"hidden" name=3D"id" value=3D"$id" /><input type=3D"hidden"
>name=3D"mother_id" value=3D"$mother_id" /><input type=3D"hidden"
>name=3D"mode" value=3D"$mode" /> <input type=3D"submit" value=3D"Absenden"
>/></form>};
>}
># Wenn Benutzername/Passwort vorhanden sind: =FCberpr=FCfen (dito zum
>lokalen Modus) elsif ($mode eq 'local' || user_check(1,
>$user_name_input, $user_password_input, $mode)) {
># Abrufen des Datensatzes, wenn eine $id vorhanden ist if ($id) {
>print qq{<h2>Editieren</h2>}; my $query =3D "SELECT mother_id,
>author_id, title, content, link,=20
>page_type, user_id, pdf, tex FROM pages WHERE page_id =3D $id";
>my $sth =3D $dbh->prepare($query); $sth->execute(); ($mother_id,
>$author_id, $title, $content, $link, $page_type, $user_id,=20
>$pdf, $tex) =3D $sth->fetchrow_array;
>$sth->finish();
>}
># sonst: Festlegen der Standardwerte f=FCr den Neueintrag else { print
>qq{<h2>Neueintrag</h2>}; ($author_id, $page_type, $user_id, $pdf,
>$tex) =3D (1,2,0,0,0);
>}
>######## print out the form here ############# print qq{<form
>action=3D"edit.pl" method=3D"post"=20
>enctype=3D"application/x-www-form-urlencoded"
>accept-charset=3D"utf-8"><input type=3D"submit" value=3D"Absenden"
>/><table><tr><td>Page_ID:</td><td><input type=3D"text" name=3D"id"
>value=3D"$id" size=3D"5" readonly=20
>/></td></tr><tr><td>Mother_ID:</td><td><input type=3D"text" size=3D"5"=20
>name=3D"mother_id" value=3D"$mother_id"=20
>/></td></tr><tr><td>Author_ID:</td><td><input type=3D"text" size=3D"5"=20
>name=3D"author_id" value=3D"$author_id"
>/></td></tr><tr><td>User_ID:</td><td><input type=3D"text" size=3D"5"
>name=3D"user_id" value=3D"$user_id"=20
>/></td></tr><tr><td>Page_Type:</td><td><input type=3D"text" size=3D"5"=20
>name=3D"page_type" value=3D"$page_type"
>/></td></tr><tr><td>Link:</td><td><input type=3D"text" size=3D"120"
>name=3D"link" value=3D"$link"=20
>/></td></tr><tr><td>PDF:</td><td><input type=3D"text" size=3D"5"
>/>name=3D"pdf"=20
>value=3D"$pdf" /></td></tr><tr><td>LaTeX:</td><td><input type=3D"text"
>size=3D"5" name=3D"tex" value=3D"$tex"
>/></td></tr><tr><td>Page_Title:</td><td><input type=3D"text"
>size=3D"120" name=3D"title" value=3D"$title"=20
>/></td></tr><tr><td>Content:</td><td><textarea name=3D"content"
>/>rows=3D"30"=20
>cols=3D"120">$content</textarea></td></tr></table><input type=3D"hidden"
>name=3D"mode" value=3D"$mode" /> <input type=3D"hidden" name=3D"user_name"
>value=3D"$user_name_input"=20
>/><input type=3D"hidden" name=3D"user_password"
>/>value=3D"$user_password_input" />=20
><input type=3D"hidden" name=3D"edit_mode" value=3D"commit" /></form>}; } #
>bei nicht erfolgreicher Identifizierung else {
>print qq{<h1>Falsches Passwort!</h1><p>Bitte verwenden Sie den=20
>"Zur=FCck"-Button Ihres Browsers, um die Eingaben zu korrigieren.};
>}
>print $page_foot unless $edit_mode eq 'commit';
>
># =DCbertragungsmodus # f=FCr erneute =DCberpr=FCfung hinzuf=FCgen: "&&
>user_check(1, $user_name_input, $user_password_input, $mode)" if
>($edit_mode eq 'commit') {
># =DCbernahme der Parameter aus der Editierungs- bzw. Neueintragsmaske
>my $mother_id =3D $q->param('mother_id'); my $author_id =3D
>$q->param('author_id'); my $user_id =3D $q->param('user_id'); my
>$page_type =3D $q->param('page_type'); my $title =3D
>$dbh->quote($q->param('title')); my $content =3D
>$dbh->quote($q->param('content')); my $link =3D
>$dbh->quote($q->param('link')); my $pdf =3D $q->param('pdf'); my $tex
>=3D $q->param('tex');
># bei vorhandener ID: Update des Datensatzes
>if ($id) { my $query =3D "UPDATE pages SET mother_id =3D $mother_id,
>author_id =3D=20
>$author_id, user_id =3D $user_id, page_type =3D $page_type, title =3D
>$title, content =3D $content, link =3D $link, pdf =3D $pdf, tex =3D $tex
>WHERE page_id =3D $id";
>my $sth =3D $dbh->prepare($query); $sth->execute(); $sth->finish();
>print $q->redirect("show.pl?mode=3Dlocal&id=3D$id");
>}
># sonst: Einf=FCgen eines neuen Datensatzes
>else { my $query =3D "INSERT INTO pages (mother_id, author_id,
>user_id, title,=20
>content, link, page_type, pdf, tex) VALUES ($mother_id, $author_id,
>$user_id, $title, $content, $link, $page_type, $pdf, $tex)";
>my $sth =3D $dbh->prepare($query); $sth->execute(); $sth->finish();
>print $q->redirect("show.pl?mode=3Dlocal&id=3D$mother_id");
>}
>}
>$dbh->disconnect; -- How many Microsoft engineers does it take to
>screw in a lightbulb? None. They just redefine "dark" as the new
>standard.
>
--=20
Hanlon's Razor: Never attribute to malice that which can be adequately expl=
ained by stupidity.
|
|
|
|
|