Example #1
0
HTTPResponse *resp_new(char *statusStr, WOInstanceHandle instHandle, WOConnection *instanceConnection)
{
   HTTPResponse *resp;
   char *status = statusStr;

   /*
    *	reformat the status line from
    *	"HTTP/1.0 200 OK Apple WebObjects" to "OK Apple..."
    */
   while (status && *status && !isspace((int)*status))	status++;
   while (*status && !isdigit((int)*status))	status++;
   if ( !(status && *status) ) {
      WOLog(WO_ERR,"Invalid response!");
      return NULL;
   }

   resp = WOCALLOC(1,sizeof(HTTPResponse));
   resp->status = atoi(status);
   resp->statusMsg = (char *)apple;
   if (strncmp(statusStr, "HTTP/1.", 7) == 0)
   {
      if (statusStr[7] == '0')
         resp->flags |= RESP_HTTP10;
      else if (statusStr[7] == '1')
         resp->flags |= RESP_HTTP11;
   }

   resp->headers = st_new(10);
   resp->instanceConnection = instanceConnection;
   resp->instHandle = instHandle;

   return resp;
}
Example #2
0
/*
 *	return a redirect response to 'path'
 */
HTTPResponse *resp_redirectedResponse(const char *path)
{
   HTTPResponse *resp;

   resp = WOCALLOC(1,sizeof(HTTPResponse));
   resp->status = 302; /* redirected */
   resp->statusMsg = WOSTRDUP("OK Apple");
   resp->headers = st_new(2);
   st_add(resp->headers, LOCATION, path, STR_COPYVALUE|STR_FREEVALUE);
   return resp;
}
Example #3
0
HTTPRequest *req_new(const char *method, char *uri)
{
   HTTPRequest *req;

   req = WOCALLOC(1,sizeof(HTTPRequest));
   //ak: adding the request method string, to be able to support other methods like HEAD, GET and POST
   req->method_str = (method ? method : "");
   req->method = get_http_method(method);
   req->request_str = uri;		/* no strdup(), but we free */
   req->shouldProcessUrl = 1;
   return req;
}
Example #4
0
HTTPResponse *resp_errorResponse(const char *msg, int status)
{
   HTTPResponse *resp;
   char buf[12];
   String *html_msg;

   resp = WOCALLOC(1,sizeof(HTTPResponse));
   resp->status = status;
   resp->statusMsg = WOSTRDUP("Error WebObjects");
   resp->headers = st_new(2);
   st_add(resp->headers, CONTENT_TYPE, DEFAULT_CONTENT, 0);
   html_msg = str_create(errorRespTextBegin, sizeof(errorRespTextBegin) + sizeof(errorRespTextEnd) + strlen(msg));
   str_append(html_msg, msg);
   str_append(html_msg, errorRespTextEnd);
   resp->content_length = resp->content_valid = resp->content_read = html_msg->length;
   resp->content = html_msg->text;
   resp_addStringToResponse(resp, html_msg);
   resp->flags |= RESP_DONT_FREE_CONTENT;
   sprintf(buf,"%d",resp->content_length);
   st_add(resp->headers, CONTENT_LENGTH, buf, STR_COPYVALUE|STR_FREEVALUE);
   return resp;
}
Example #5
0
void *WOShmem_alloc(const char *regionName, size_t elementSize, unsigned int *elementCount)
{
   Region *r, *newRegion;
   int found, nameLen;
   void *lockHandle, *ret = NULL;

   if (WOShmem_fd == -1)
   {
      /* There was some problem mapping the shared file. Fall back to using private storage so the adaptor still works. */
      WOLog(WO_ERR, "WOShmem_alloc(): shared memory disabled - mallocing instead (%s)", regionName);
      return WOCALLOC(*elementCount, elementSize);
   }

   /* Scan the region list looking for regionName. */
   /* Note that since the file was initialized with zeros, the first lookup will not */
   /* find any region, and will create the first region at offset 0. */
   r = (Region *)offset_to_addr(0);
   found = 0;
   do {
      lockHandle = WOShmem_lock(r, sizeof(Region), 0);
      if (strcmp(regionName, r->name) == 0)
      {
         found = 1;
         /* validate the element size */
         if (r->elementSize == elementSize)
         {
            *elementCount = r->elementCount;
            ret = offset_to_addr(r->offset);
            WOLog(WO_INFO, "WOShmem_alloc(): found region \"%s\" (%d x %d)", regionName, elementSize, *elementCount);
         } else {
            WOLog(WO_ERR, "WOShmem_alloc(): size mismatch in region %s: %d vs %d", regionName, r->elementSize, elementSize);
         }
      } else {
         if (r->nextRegion == 0)
         {
            /* r points to the last defined region, which is still locked shared. Need an exclusive lock. */
            WOShmem_unlock(lockHandle);
            lockHandle = WOShmem_lock(r, sizeof(Region), 1);
            /* Be sure that nobody else got in ahead of our exclusive lock */
            /* If they did, just resume the loop to check the region that somebody else just created. */
            if (r->nextRegion == 0)
            {
               /* now we can allocate a new region after the last existing one */
               /* first check that there is enough room for the desired size */
               nameLen = strlen(regionName);
               /* keep things aligned to 16 byte boundary by padding out the name */
               if ((sizeof(Region)+nameLen) % 16)
                  nameLen += 16 - (sizeof(Region)+nameLen) % 16;
               if (r->offset + r->elementSize * r->elementCount + sizeof(Region) + nameLen + elementSize * *elementCount < WOShmem_size)
               {
                  /* enough room; create the new region */
                  r->nextRegion = r->offset + r->elementSize * r->elementCount;
                  newRegion = (Region *)offset_to_addr(r->nextRegion);
                  newRegion->offset = r->nextRegion + sizeof(Region) + nameLen;
                  newRegion->elementSize = elementSize;
                  newRegion->elementCount = *elementCount;
                  newRegion->nextRegion = 0;
                  strcpy(&newRegion->name[0], regionName);
                  ret = offset_to_addr(newRegion->offset);
                  WOLog(WO_INFO, "WOShmem_alloc(): allocated region \"%s\" (%d x %d)", regionName, elementSize, *elementCount);
               } else {
                  /* not enough room for new region */
                  WOLog(WO_ERR, "WOShmem_alloc(): not enough shared memory to allocate region \"%s\" (%d x %d)", regionName, elementSize, *elementCount);
               }
               found = 1;
            }
         }
         r = (Region *)offset_to_addr(r->nextRegion);
      }
      WOShmem_unlock(lockHandle);
   } while (!found);
   return ret;
}