Bug 29725 - [picobsd] [patch] fixed segmentation fault in simple_httpd and some other bugfixes
Summary: [picobsd] [patch] fixed segmentation fault in simple_httpd and some other bug...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 3.5-STABLE
Hardware: Any Any
: Normal Affects Only Me
Assignee: dwmalone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2001-08-15 11:10 UTC by Eugene Grosbein
Modified: 2008-08-16 16:00 UTC (History)
0 users

See Also:


Attachments
file.diff (3.86 KB, patch)
2001-08-15 11:10 UTC, Eugene Grosbein
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Eugene Grosbein 2001-08-15 11:10:17 UTC
This PR fixes some bugs in /usr/src/release/picobsd/simple_httpd
and tries to clean up code.

* Fixed segmentation fault when serving request of 
  file without '.' in the name. Added file mime_t.h introducing 
  array mime_type[][] for mapping of file extentions to mime types 
  and string default_mime_type for unknown/missing extentions.
* The 'Content-length' header prepared but not sent - fixed.
* Fixed format string in init_servconnection() - missing "%d" added.
* Fixed format string for value of type off_t in http_request() -
  "%d" replaced with "%lld"
* Commented out unused variables ld, http1, msg in http_request()
* Commented out unused variables bflad, fd, lg in main()

Fix: There are a patch for simple_httpd.c and new file mime_t.h for
including into tree. Feel free editing there but please commit fixes
for bugs.

/* start of mime_t.h */
#ifndef _MIME_T
#define _MIME_T

const char* default_mime_type = "application/octet-stream";

const char* mime_type[][2] = {
  { "txt",      "text/plain"            },
  { "htm",      "text/html"             },
  { "html",     "text/html"             },
  { "gif",      "image/gif"             },
  { "jpg",      "image/jpeg"            },
  { "mp3",      "audio/mpeg"            }
};

const int mime_type_max = sizeof(mime_type) / sizeof(mime_type[0]) - 1;

#endif
/* end of mime_t.h */

Here is a patch for simple_httpd.c from my local repository:
How-To-Repeat: Build, configure and run simple_httpd.
Connect to it using telnet and try to 'GET' a file with an extention -
you will get no 'Content-length' header in the reply.
Try to 'GET' a file with no extention - you will get no file
and coredump of simple_httpd.
Comment 1 Jeroen Ruigrok van der Werven freebsd_committer 2002-04-07 21:06:23 UTC
State Changed
From-To: open->analyzed

I am already working on this.
Comment 2 Johan Karlsson freebsd_committer 2002-04-24 01:58:03 UTC
Responsible Changed
From-To: freebsd-bugs->asmodai

Can you close this when you are done with  
the changes to simple_httpd, please.
Comment 3 Marc Fonvieille freebsd_committer 2002-08-05 19:23:20 UTC
Responsible Changed
From-To: asmodai->freebsd-bugs

asmodai requested it.
Comment 4 Eugene Grosbein 2003-01-01 06:56:12 UTC
Here is an updated version of simple_httpd and ChangeLog therein:
ftp://www.kuzbass.ru/pub/freebsd/simple_httpd.tar.bz2

All found bugs are fixed, added a couple of features, still tiny size
of source and compiled code, performance improved.
Comment 5 Eugene Grosbein 2003-03-01 16:44:21 UTC
Hi!

asmodai resigned longh time ago, so this PR is not analyzed really.
Please change its state back to `open', I hope somebody will take it then.

Eugene Grosbein
Comment 6 Giorgos Keramidas freebsd_committer 2003-03-05 01:14:30 UTC
State Changed
From-To: analyzed->open

Reopen this.
Comment 7 dwmalone freebsd_committer 2003-03-13 21:58:22 UTC
Responsible Changed
From-To: freebsd-bugs->dwmalone

I'll have a look at this.
Comment 8 Eugene Grosbein 2003-05-13 05:15:01 UTC
Hi!

Do you plan to deal with this PR?

Eugene Grosbein
Comment 9 Eugene Grosbein 2004-08-08 15:51:57 UTC
Hi!

It seems that dwmalone@freebsd.org does not have time to deal with this PR -
a year is gone since he touched it. Please change responsibility
back to freebsd-bugs, I hope somebody else will take it.

Eugene Grosbein
Comment 10 dwmalone 2004-08-08 17:50:22 UTC
> It seems that dwmalone@freebsd.org does not have time to deal with this PR -
> a year is gone since he touched it. Please change responsibility
> back to freebsd-bugs, I hope somebody else will take it.

Doh! Sorry - I've finished cleaning up the patch and I've included
the mime type bits of the patch are below. If you think it looks
OK, I'll commit it this evening. The changes from your original
patch are:

1) Don't make a mime_types.h 'cos we should avoid creating variables
   in header files (declaring them is OK, but we were defining them).
2) Use strrchr to find the extension, rather than strchr.
3) Slightly simplify the mime-type matching loop.

I think I had committed the other bits of the patch already?

	David.


Index: Makefile
===================================================================
RCS file: /cvs/FreeBSD-CVS/src/release/picobsd/tinyware/simple_httpd/Makefile,v
retrieving revision 1.6
diff -u -r1.6 Makefile
--- Makefile	28 Aug 1999 01:33:59 -0000	1.6
+++ Makefile	8 Aug 2004 16:02:49 -0000
@@ -3,5 +3,7 @@
 PROG=simple_httpd
 SRCS= 	simple_httpd.c
 NOMAN=yes
+WARNS?=6
+CFLAGS+=-g -O2
 
 .include <bsd.prog.mk>
Index: simple_httpd.c
===================================================================
RCS file: /cvs/FreeBSD-CVS/src/release/picobsd/tinyware/simple_httpd/simple_httpd.c,v
retrieving revision 1.15
diff -u -r1.15 simple_httpd.c
--- simple_httpd.c	5 Apr 2003 17:15:38 -0000	1.15
+++ simple_httpd.c	8 Aug 2004 16:38:43 -0000
@@ -71,6 +71,19 @@
 
 static char http_200[] = "HTTP/1.0 200 OK\r";
 
+const char *default_mime_type = "application/octet-stream";
+
+const char *mime_type[][2] = {
+    { "txt",      "text/plain"            },
+    { "htm",      "text/html"             },
+    { "html",     "text/html"             },
+    { "gif",      "image/gif"             },
+    { "jpg",      "image/jpeg"            },
+    { "mp3",      "audio/mpeg"            }
+};
+
+const int mime_type_max = sizeof(mime_type) / sizeof(mime_type[0]) - 1;
+
 /* Two parts, HTTP Header and then HTML */
 static const char *http_404[2] = 
     {"HTTP/1.0 404 Not found\r\n", 
@@ -205,7 +218,7 @@
 	int             fd, lg, i; 
 	int             cmd = 0;
 	char           *p, *par;
-	const char     *filename, *c;
+	const char     *filename, *c, *ext, *type;
 	struct stat     file_status;
 	char            req[1024];
 	char            buff[8192];
@@ -321,21 +334,17 @@
 	sprintf(buff, "Content-length: %lld\r\n", file_status.st_size);
 	write(con_sock, buff, strlen(buff));
 
-	if (strstr(filename,".txt")) {
-	  strcpy(buff,"Content-type: text/plain\r\n");
-	} else if (strstr(filename,".html") || strstr(filename,".htm")) {
-	    strcpy(buff,"Content-type: text/html\r\n");
-	} else if (strstr(filename,".gif")) {
-	  strcpy(buff,"Content-type: image/gif\r\n");
-	} else if (strstr(filename,".jpg")) {
-	  strcpy(buff,"Content-type: image/jpeg\r\n");
-	} else {
-	  /* Take a guess at content if we don't have something already */
-	  strcpy(buff,"Content-type: ");
-	  strcat(buff,strstr(filename,".")+1);
-	  strcat(buff,"\r\n");
+	strcpy(buff, "Content-type: ");
+	type = default_mime_type;
+	if ((ext = strrchr(filename, '.')) != NULL) {
+	  for (i = mime_type_max; i >= 0; i--)
+	    if (strcmp(ext + 1, mime_type[i][0]) == 0) {
+	      type = mime_type[i][1];
+	      break;
+	    }
 	}
-	write(con_sock, buff, strlen(buff));
+	strcat(buff, type);
+	http_output(buff);
 	
 	strftime(buff, 50, "Last-Modified: %a, %d %h %Y %H:%M:%S %Z\r\n\r\n", gmtime(&file_status.st_mtime));
 	write(con_sock, buff, strlen(buff));
Comment 11 dwmalone freebsd_committer 2008-08-16 16:00:00 UTC
State Changed
From-To: open->closed

Seems that I committed this ages ago. 

David.