LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-03-2017, 01:21 AM   #1
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Rep: Reputation: 52
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.
 
Old 07-03-2017, 02:15 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You forgot to attach the relevant code, also the problematic output.
 
Old 07-05-2017, 03:08 AM   #3
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
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
 
2 members found this post helpful.
Old 07-05-2017, 04:53 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,728

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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?

Last edited by scasey; 07-05-2017 at 04:56 PM.
 
1 members found this post helpful.
Old 07-06-2017, 12:36 AM   #5
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Original Poster
Rep: Reputation: 52
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.

Last edited by rblampain; 07-06-2017 at 12:58 AM.
 
Old 07-06-2017, 05:56 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
How about sending 'HTTP/1.0 200 OK' first?
 
1 members found this post helpful.
Old 07-06-2017, 08:57 AM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,728

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by NevemTeve View Post
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.

Last edited by scasey; 07-06-2017 at 09:05 AM.
 
1 members found this post helpful.
Old 07-06-2017, 09:42 AM   #8
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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
 
3 members found this post helpful.
Old 07-06-2017, 12:13 PM   #9
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,728

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by Guttorm View Post
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.

Last edited by scasey; 07-06-2017 at 02:23 PM.
 
2 members found this post helpful.
Old 07-06-2017, 09:20 PM   #10
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
That is probably the problem. But I wouldn't add .asm to CGI programs, just assemble it to a binary ending in .cgi.
 
3 members found this post helpful.
Old 07-06-2017, 10:05 PM   #11
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,728

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by Laserbeak View Post
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
 
2 members found this post helpful.
Old 07-07-2017, 06:35 AM   #12
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Also, make sure it's in the right "cgi-bin" directory/folder.
 
1 members found this post helpful.
Old 07-07-2017, 11:39 AM   #13
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Quote:
Originally Posted by scasey View Post
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.
 
2 members found this post helpful.
Old 07-07-2017, 01:19 PM   #14
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
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".
 
1 members found this post helpful.
Old 07-07-2017, 02:12 PM   #15
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,728

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by Laserbeak View Post
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)

Last edited by scasey; 07-07-2017 at 02:18 PM.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Laserjet 6P printing pages of garbage shane601 Linux - Hardware 4 11-03-2009 03:58 PM
Printing garbage, not sure what to do John Teisberg Linux - Newbie 1 03-29-2009 08:11 AM
How to stop printer from printing garbage greengrocer Linux - Newbie 5 10-22-2005 11:25 PM
Printers printing garbage? inkysplat Linux - Hardware 1 11-03-2003 02:27 PM
Printing garbage sakkie Slackware 1 09-19-2003 12:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration