HTTP.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2003 Sony Corporation 
00003 //
00004 // Permission to use, copy, modify, and redistribute this software for
00005 // non-commercial use is hereby granted.
00006 //
00007 // This software is provided "as is" without warranty of any kind,
00008 // either expressed or implied, including but not limited to the
00009 // implied warranties of fitness for a particular purpose.
00010 //
00011 
00012 #include <stdio.h>
00013 #include <string.h>
00014 #include "HTTP.h"
00015 
00016 struct http_code {
00017         int num;
00018         char *str;
00019         int len;
00020 };
00021 
00022 static const http_code _http_status[] = {
00023         {100, "Continue",               sizeof("Continue")},
00024         {101, "Switching Protocols",    sizeof("Switching Protocols")},
00025         {200, "Ok",                     sizeof("Ok")},
00026         {201, "Created",                sizeof("Created")},
00027         {202, "Accepted",               sizeof("Accepted")},
00028         {204, "No Content",             sizeof("No Content")},
00029         {400, "Bad Request",            sizeof("Bad Request")},
00030         {400, "Not Found",              sizeof("Not Found")},
00031         {500, "Internal Server Error",  sizeof("Internal Server Error")},
00032         {501, "Not Implemented",        sizeof("Not Implemented")},
00033 };
00034 
00035 static const http_code _http_header[] = {
00036         {HTTP_SERVER,         "Server: ",         sizeof ("Server: ")},
00037         {HTTP_CONTENT_TYPE,   "Content-Type: ",   sizeof ("Content-type: ")},
00038         {HTTP_CONTENT_LENGTH, "Content-Length: ", sizeof ("Content-length: ")},
00039         {HTTP_MIME_VERSION,   "MIME-Version: ",   sizeof ("MIME-Version: ")},
00040         {HTTP_EXPIRES,        "Expires: ",        sizeof ("Expires: ")},
00041         {HTTP_DATE,           "Date: ",           sizeof ("Date: ")},
00042         {HTTP_HEADER_END,     "\r\n",             sizeof ("\r\n")},
00043 };
00044 
00045 bool
00046 HTTP::Parse(char* httpReq, char* method, char* uri, char* httpVer)
00047 {
00048     int len;
00049     char* start;
00050     char* end;
00051 
00052     // Method
00053     start = end = httpReq;
00054     end   = httpReq;
00055     while (*end != ' ' && *end != '\0') end++;
00056     if (*end == '\0') return false;
00057     len = end - start;
00058     memcpy(method, start, len);
00059     method[len] = '\0';
00060 
00061     // URI
00062     end++;
00063     start = end;
00064     while (*end != ' ' && *end != '\0') end++;
00065     if (*end == '\0') return false;
00066     len = end - start;
00067     memcpy(uri, start, len);
00068     uri[len] = '\0';
00069   
00070     // HTTP version
00071     end++;
00072     start = end;
00073     while (*end != '\r' && *end != '\0') end++;
00074     if (*end == '\0') return false;
00075     len = end - start;
00076     memcpy(httpVer, start, len);
00077     httpVer[len] = '\0';
00078 
00079     return true;
00080 }
00081 
00082 int 
00083 HTTP::Status(char* dest, HTTPStatus st)
00084 {
00085         const http_code* code = &_http_status[st];
00086 
00087         sprintf (buffer, "%s %d %s\r\n", HTTP_VERSION, code->num, code->str);
00088     int status_length = strlen(buffer);
00089     memcpy(dest, buffer, status_length);
00090 
00091         return status_length;
00092 }
00093 
00094 int
00095 HTTP::HeaderField(char* dest, HTTPHeader hdr, char *value)
00096 {
00097         const http_code* code = &_http_header[hdr];
00098     int header_length = 0;
00099         int len;
00100 
00101         sprintf (buffer, "%s", code->str);
00102     len = strlen(buffer);
00103     memcpy(dest, buffer, len);
00104     dest += len;
00105     header_length += len;
00106 
00107         if (value) {
00108                 len = strlen (value);
00109                 if (len > MAX_HEADER_SIZE)
00110                         value = "-- buffer too short! --";
00111                 sprintf (buffer, "%s\r\n", value);
00112         len = strlen(buffer);
00113         memcpy(dest, buffer, len);
00114         header_length += len;
00115         }
00116 
00117         return header_length;
00118 }

Generated on Sun Dec 2 23:04:28 2007 for openSDK by  doxygen 1.3.9.1