<commit_before>/*
   Copyright 2013 The Trustees of Princeton University

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/


#include "libsyndicate/httpd.h"

char const MD_HTTP_NOMSG[128] = "\n";
char const MD_HTTP_200_MSG[128] = "OK\n";
char const MD_HTTP_302_MSG[128] = "Redirect\n";
char const MD_HTTP_400_MSG[128] = "Bad Request\n";
char const MD_HTTP_401_MSG[128] = "Invalid authorization credentials\n";
char const MD_HTTP_403_MSG[128] = "Credentials required\n";
char const MD_HTTP_404_MSG[128] = "Not found\n";
char const MD_HTTP_409_MSG[128] = "Operation conflict\n";
char const MD_HTTP_413_MSG[128] = "Requested entry too big\n";
char const MD_HTTP_422_MSG[128] = "Unprocessable entry\n";
char const MD_HTTP_500_MSG[128] = "Internal Server Error\n";
char const MD_HTTP_501_MSG[128] = "Not implemented\n";
char const MD_HTTP_502_MSG[128] = "Bad gateway\n";
char const MD_HTTP_503_MSG[128] = "Service unavailable\n";
char const MD_HTTP_504_MSG[128] = "Remote Server Timeout\n";

char const MD_HTTP_DEFAULT_MSG[128] = "RESPONSE\n";


// respond to a request
static int md_HTTP_default_send_response( struct MHD_Connection* connection, int status_code, char* data ) {
   char const* page = NULL;
   struct MHD_Response* response = NULL;
   
   if( data == NULL ) {
      // use a built-in status message
      switch( status_code ) {
         case MHD_HTTP_BAD_REQUEST:
            page = MD_HTTP_400_MSG;
            break;
         
         case MHD_HTTP_INTERNAL_SERVER_ERROR:
            page = MD_HTTP_500_MSG;
            break;
         
         case MHD_HTTP_UNAUTHORIZED:
            page = MD_HTTP_401_MSG;
            break;
            
         default:
            page = MD_HTTP_DEFAULT_MSG;
            break;
      }
      response = MHD_create_response_from_buffer( strlen(page), (void*)page, MHD_RESPMEM_PERSISTENT );
   }
   else {
      // use the given status message
      response = MHD_create_response_from_buffer( strlen(data), (void*)data, MHD_RESPMEM_MUST_FREE );
   }
   
   if( !response )
      return MHD_NO;
      
   // this is always a text/plain type
   MHD_add_response_header( response, "Content-Type", "text/plain" );
   int ret = MHD_queue_response( connection, status_code, response );
   MHD_destroy_response( response );
   
   return ret;
}

// make a RAM response which we'll defensively copy
int md_create_HTTP_response_ram( struct md_HTTP_response* resp, char const* mimetype, int status, char const* data, int len ) {
   resp->resp = MHD_create_response_from_buffer( len, (void*)data, MHD_RESPMEM_MUST_COPY );
   resp->status = status;
   MHD_add_response_header( resp->resp, "Content-Type", mimetype );
   return 0;
}

// make a RAM response which we'll keep a pointer to and free later
int md_create_HTTP_response_ram_nocopy( struct md_HTTP_response* resp, char const* mimetype, int status, char const* data, int len ) {
   resp->resp = MHD_create_response_from_buffer( len, (void*)data, MHD_RESPMEM_MUST_FREE );
   resp->status = status;
   MHD_add_response_header( resp->resp, "Content-Type", mimetype );
   return 0;
}

// make a RAM response which is persistent
int md_create_HTTP_response_ram_static( struct md_HTTP_response* resp, char const* mimetype, int status, char const* data, int len ) {
   resp->resp = MHD_create_response_from_buffer( len, (void*)data, MHD_RESPMEM_PERSISTENT );
   resp->status = status;
   MHD_add_response_header( resp->resp, "Content-Type", mimetype );
   return 0;
}

// make an FD-based response<commit_msg>Better method documentation on how libsyndicate's HTTP server can generate responses.<commit_after>/*
   Copyright 2013 The Trustees of Princeton University

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/


#include "libsyndicate/httpd.h"

char const MD_HTTP_NOMSG[128] = "\n";
char const MD_HTTP_200_MSG[128] = "OK\n";
char const MD_HTTP_302_MSG[128] = "Redirect\n";
char const MD_HTTP_400_MSG[128] = "Bad Request\n";
char const MD_HTTP_401_MSG[128] = "Invalid authorization credentials\n";
char const MD_HTTP_403_MSG[128] = "Credentials required\n";
char const MD_HTTP_404_MSG[128] = "Not found\n";
char const MD_HTTP_409_MSG[128] = "Operation conflict\n";
char const MD_HTTP_413_MSG[128] = "Requested entry too big\n";
char const MD_HTTP_422_MSG[128] = "Unprocessable entry\n";
char const MD_HTTP_500_MSG[128] = "Internal Server Error\n";
char const MD_HTTP_501_MSG[128] = "Not implemented\n";
char const MD_HTTP_502_MSG[128] = "Bad gateway\n";
char const MD_HTTP_503_MSG[128] = "Service unavailable\n";
char const MD_HTTP_504_MSG[128] = "Remote Server Timeout\n";

char const MD_HTTP_DEFAULT_MSG[128] = "RESPONSE\n";


// respond to a request
static int md_HTTP_default_send_response( struct MHD_Connection* connection, int status_code, char* data ) {
   char const* page = NULL;
   struct MHD_Response* response = NULL;
   
   if( data == NULL ) {
      // use a built-in status message
      switch( status_code ) {
         case MHD_HTTP_BAD_REQUEST:
            page = MD_HTTP_400_MSG;
            break;
         
         case MHD_HTTP_INTERNAL_SERVER_ERROR:
            page = MD_HTTP_500_MSG;
            break;
         
         case MHD_HTTP_UNAUTHORIZED:
            page = MD_HTTP_401_MSG;
            break;
            
         default:
            page = MD_HTTP_DEFAULT_MSG;
            break;
      }
      response = MHD_create_response_from_buffer( strlen(page), (void*)page, MHD_RESPMEM_PERSISTENT );
   }
   else {
      // use the given status message
      response = MHD_create_response_from_buffer( strlen(data), (void*)data, MHD_RESPMEM_MUST_FREE );
   }
   
   if( !response )
      return MHD_NO;
      
   // this is always a text/plain type
   MHD_add_response_header( response, "Content-Type", "text/plain" );
   int ret = MHD_queue_response( connection, status_code, response );
   MHD_destroy_response( response );
   
   return ret;
}

// make a RAM response which MHD will defensively copy
int md_create_HTTP_response_ram( struct md_HTTP_response* resp, char const* mimetype, int status, char const* data, int len ) {
   resp->resp = MHD_create_response_from_buffer( len, (void*)data, MHD_RESPMEM_MUST_COPY );
   resp->status = status;
   MHD_add_response_header( resp->resp, "Content-Type", mimetype );
   return 0;
}

// make a RAM response which MHD keep a pointer to and free later
int md_create_HTTP_response_ram_nocopy( struct md_HTTP_response* resp, char const* mimetype, int status, char const* data, int len ) {
   resp->resp = MHD_create_response_from_buffer( len, (void*)data, MHD_RESPMEM_MUST_FREE );
   resp->status = status;
   MHD_add_response_header( resp->resp, "Content-Type", mimetype );
   return 0;
}

// make a RAM response which MHD should not copy, but the caller will not free
int md_create_HTTP_response_ram_static( struct md_HTTP_response* resp, char const* mimetype, int status, char const* data, int len ) {
   resp->resp = MHD_create_response_from_buffer( len, (void*)data, MHD_RESPMEM_PERSISTENT );
   resp->status = status;
   MHD_add_response_header( resp->resp, "Content-Type", mimetype );
   return 0;
}

// make an file-descriptor-based response
{'fraction_non_alphanumeric': 0.09095378564405113, 'fraction_numerical': 0.02704031465093412, 'mean_word_length': 3.6818181818181817, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2061393', 'n_tokens_mistral': 2755, 'n_tokens_neox': 2581, 'n_words': 770}