assembly cgi program printing garbage
I have this old assembly program (nasm64) that does a good job of comparing strings in a particular way outside of CGI and I am trying to make it work as a CGI. It works correctly from the CLI but prints the typical square black boxes and question marks as a CGI (through a HTML/browser-document and Apache2), this string of garbage is always proportional to the length of the strings tested, so it is trying to print the string. In my test, ASCII strings are used but the program is intended to be used for true (non-ascii) utf-8 strings.
The same code in BASIC works perfectly (ascii tested again although the result should be the same with UTF-8 on a byte-by-byte comparison basis) but can't get the length given by CGI variable of the returned strings (how many bytes to read) and is too slow at string comparisons. I have insufficient experience in C, Perl, PHP or Python etc and only a basic knowledge of Apache2 and this strings comparison is a real bottleneck in need of assembly code that I find is my fastest way of solving that sort of problems. My questions are: What should I be looking for to solve the problem? Is Apache2 mis-configured/not-configured? I thought it was configured for UTF-8 by default and should automatically print through system call 1 (write) anything printable. Or could it be a GDM3 problem? Or a combination of both? Or do I miss something? I assume the same code in C would give the same result so a solution for C would probably solve the problem in Assembly code. Thank you for your help. Unmodified Debian 7 and Apache2 from DVD installation. New Gigabyte mobo AB350 with Feb 2017 BIOS. |
You forgot to attach the relevant code, also the problematic output.
|
Are you printing the correct HTTP headers including Content-Type, Content-Length, etc.
If you're using UTF-8, you should send Content-Type: text/html; charset=utf-8 or Content-Type: text/plain; charset=utf-8 |
Apache is generally not configured for UTF-8 by default. Supplying the charset as Laserbeak mentioned would change it for that page.
You do understand that your CGI script needs to supply HTML output in order to be viewed in a browser. At a very minimum, it needs to send Code:
Content-type: text/html Code:
<html> And to repeat NevemTeve's question...may we see the code and the output, please? |
Thank you for the answers. My apologies, I was wrongly convinced it was so simple I did not think code was necessary, I also have to do this from the local library.
Code:
bits 64 result through browser in asm Code:
��H���W(�/�IQ� Code:
Content-type: text/html Code:
#include "stdio.h" Code:
��H���W(�/�IQP� Code:
Content-Type: text/html Code:
Content-Type: text/html; charset=utf-8 |
How about sending 'HTTP/1.0 200 OK' first?
|
Quote:
|
To me, it looks like Apache is simply outputting the binary instead of executing it.
https://httpd.apache.org/docs/2.4/howto/cgi.html |
Quote:
rblampain, please show us how you are calling your program from the web page. Edit: I just remembered why that happens. (tho I just saw that this is in the link Guttorm posted) The setting Code:
AddHandler cgi-script .cgi .pl |
That is probably the problem. But I wouldn't add .asm to CGI programs, just assemble it to a binary ending in .cgi.
|
Quote:
Sorry, I get caught up in the "root cause" and forget the simple solutions -- making the solution fit the conditions instead of 'tother way 'round...I also forget that some people might not have access to the httpd.conf |
Also, make sure it's in the right "cgi-bin" directory/folder.
|
Quote:
|
So this is only DOS/Windows compatible? I tried to assemble it and it wouldn't run. When I ran "file" on it, it said "COM executable for DOS".
|
Quote:
Code:
Server error! |
Quote:
|
> I tried to assemble it and it wouldn't run.
How did you try to assemble it? What command did you use? |
Quote:
Code:
Code:
| => nasm -f macho64 -o test.cgi cgi.asm Code:
| => uname -a LOL I can almost run it this way, but it's in reverse order: Code:
| => strings test.cgi |
I compiled it in Solaris:
Code:
root@sunshine:~# uname -a Code:
root@sunshine:~# nasm -f elf64 -o test.cgi test.asm Code:
root@sunshine:~# ./test.cgi |
I would try link it:
Code:
nasm -f elf64 -o test.o test.asm |
Quote:
It did not work for Mac OS X replacing -f elf64 with -f macho64 though |
Works for me:
Code:
uname -mr |
Quote:
Code:
nasm -f elf64 test.asm -o test.o This code does not call the server and also work in my case Quote:
I have used verbatim a more involved example from the NCSA (32bit) and it does not work either - same result. |
Works for me:
Code:
$ ls -l /usr/local/www/cgi-bin/rb_cgi |
Quote:
Without a matching extension, apache will not execute the script/program, but will just send the contents of the file, which in your case, is a binary file. Please add a .cgi extension to your compiled file and try again. I don't believe it needs to be re-compiled with the extension, the file just has to have that name for apache to execute it instead of delivering it. |
I believe if it is in a designated cgi-bin directory the web server should try to execute the file and if that fails for some reason (anything from not being able to execute it at all to returning no header or a bad header) it should just give a server error. It shouldn't send the contents of the file, which means wherever the file was, it wasn't in a properly declared cgi-bin directory.
|
As a last straw, one could check log-files access_log and error_log in directory /var/log/apache2
|
Quote:
Code:
Option ExecCGI Code:
AddHandler cgi-script .cgi .pl When a ScriptAlias directive is defined for a folder, then neither of the other directives are required. I believe I'll do some tweaking of the config file over the next couple of days. Thank you, Laserbeak and NevemTeve :hattip: rblampain, what I've been saying will work, it's just not the only way, and probably not the best way. Please confirm that your cgi-bin does have a ScriptAlias defined for it. Something like: Code:
ScriptAlias /cgi-bin/ /path/to/the/cgi-bin/ Code:
# ScriptAlias: This controls which directories contain server scripts. |
I cannot understand what the problem was. Initially having "charset=urf-8" (maybe not the exact spelling) in the header seemed to make it worse, so I took it out on the assumption that I only had ascci in the testing files. In my last test, I put that back and it works and I have no idea what the difference is. There was no need to alter any configuration, as explained in my previous post.
|
All times are GMT -5. The time now is 08:39 PM. |