LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   assembly cgi program printing garbage (https://www.linuxquestions.org/questions/programming-9/assembly-cgi-program-printing-garbage-4175609062/)

rblampain 07-03-2017 01:21 AM

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.

NevemTeve 07-03-2017 02:15 AM

You forgot to attach the relevant code, also the problematic output.

Laserbeak 07-05-2017 03:08 AM

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

scasey 07-05-2017 04:53 PM

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
as the first thing...the blank line is significant. It should also send:
Code:

<html>
<head>
<meta charset="utf-8">
</head>
<body>
...
</body>
</html>

where ... represents the output of the CGI and whatever HTML formatting might be also required...although only the meta tag is required to address your question. Please let us know how that works for you.

And to repeat NevemTeve's question...may we see the code and the output, please?

rblampain 07-06-2017 12:36 AM

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

global _start
section .text
_start:                                ; ELF entry point
;; print http header
;; =================
mov rax, 1                            ; sys_write
mov rdi, 1                            ; STDOUT
mov rsi, http_header              ; buffer
mov rdx, http_headerLen            ; length of buffer
syscall
;; print text
;; ==========
mov rax, 1                            ; sys_write
mov rdi, 1                            ; STDOUT
mov rsi, sometext                      ; buffer
mov rdx, sometextLen                ; length of buffer
syscall

mov rax, 60                            ; sys_exit
mov rdi, 0                            ; 0
syscall

section .data

sometext        :        db "Hello world!", 0Ah
sometextLen EQU $ - sometext

http_header: db        'Content-type: text/html', 0Ah, 0Ah
http_headerLen EQU $ - http_header

note: tried mov rdx, [sometextLen] - no good either

result through browser in asm
Code:

��H���W(�/�IQ�
result through CLI in asm
Code:

Content-type: text/html

Hello world!


Code:

#include "stdio.h"
 
int main(void) {
  printf( "Content-Type: text/html\n\n" );
  printf("Hello world !\n");
  return 0;
}

result through browser with C
Code:

��H���W(�/�IQP�
result through CLI with C
Code:

Content-Type: text/html

Hello world !

In nasm (version coming with Debian 7)
Code:

Content-Type: text/html; charset=utf-8
makes if freeze, I'll try to specify UTF-8 in the 'meta' line as suggested.

NevemTeve 07-06-2017 05:56 AM

How about sending 'HTTP/1.0 200 OK' first?

scasey 07-06-2017 08:57 AM

Quote:

Originally Posted by NevemTeve (Post 5731345)
How about sending 'HTTP/1.0 200 OK' first?

That comes from Apache in response to a request. It doesn't need to be programmed in the CGI program/script.

Guttorm 07-06-2017 09:42 AM

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

scasey 07-06-2017 12:13 PM

Quote:

Originally Posted by Guttorm (Post 5731435)
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

That's an excellent point!! I hadn't considered it because my CGI scripts are always plain text (perl) files, so when that happens to me it's very obvious.

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
in httpd.conf needs to include the suffix of the file to be executed, so if its .asm - add that to the AddHandler directive. Otherwise, apache doesn't know to execute the code, and just delivers it instead.

Laserbeak 07-06-2017 09:20 PM

That is probably the problem. But I wouldn't add .asm to CGI programs, just assemble it to a binary ending in .cgi.

scasey 07-06-2017 10:05 PM

Quote:

Originally Posted by Laserbeak (Post 5731728)
That is probably the problem. But I wouldn't add .asm to CGI programs, just assemble it to a binary ending in .cgi.

Good point. The OP probably already has a binary; it just needs to be renamed as something.cgi.

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

Laserbeak 07-07-2017 06:35 AM

Also, make sure it's in the right "cgi-bin" directory/folder.

Laserbeak 07-07-2017 11:39 AM

Quote:

Originally Posted by scasey (Post 5731106)
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

This is incorrect. If you send "Content-Type: text/plain" in the header, any browser will just display it as plain text. You do not have to send HTML.

Laserbeak 07-07-2017 01:19 PM

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".

scasey 07-07-2017 02:12 PM

Quote:

Originally Posted by Laserbeak (Post 5732002)
This is incorrect. If you send "Content-Type: text/plain" in the header, any browser will just display it as plain text. You do not have to send HTML.

Yes, that's true. Point is that the first thing from the script needs to be a "Content-Type:..." header followed by a blank line. If that's not present, the browser will report an error 500:
Code:

Server error!

The server encountered an internal error and was unable to complete your request.

Error message:
malformed header from script. Bad header=<html>: popup.pl

If you think this is a server error, please contact the webmaster.
Error 500

(this example is a script without the header so the first thing it sends is the <html> tag)


All times are GMT -5. The time now is 09:56 PM.