bool NodeXQType::is_equal(const TypeManager* tm, const NodeXQType& other) const { if (theNodeKind != other.theNodeKind) return false; if (theNodeName != other.theNodeName) { if (theNodeName == NULL || other.theNodeName == NULL) return false; if (!theNodeName->equals(other.theNodeName)) return false; } xqtref_t c1 = get_content_type(); xqtref_t c2 = other.get_content_type(); if (c1 == c2) return true; if (c1 != NULL && c2 != NULL) return TypeOps::is_equal(tm, *c1, *c2); if (c1 == NULL) return (c2->type_kind() == XQType::ANY_TYPE_KIND); else return (c1->type_kind() == XQType::ANY_TYPE_KIND); return false; }
unicode DocumentView::guess_mimetype() const { if(is_new_file()) { return "text/plain"; } auto info = file_->query_info(G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE); return info->get_content_type(); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr)) { PSOCK_BEGIN(&s->sout); SEND_STRING(&s->sout, statushdr); SEND_STRING(&s->sout, get_content_type(s->filename)); PSOCK_END(&s->sout); }
static int send_f(mihl_cnx_t * cnx, char const *tag, char const *host, const char *filename) { char path[512]; const char *content_type = get_content_type(filename); if (strstr(filename, "..")) { return -1; } snprintf(path, sizeof(path), "%s/%s", ROOT, filename); return send_file(cnx, tag, path, content_type, 0); }
/*! * \brief Release memory allocated for the global web server root directory * and the global XML document. Resets the flag bWebServerState to * WEB_SERVER_DISABLED. * * \return Integer. */ static int get_file_info( /*! [in] Filename having the description document. */ const char *filename, /*! [out] File information object having file attributes such as filelength, * when was the file last modified, whether a file or a directory and * whether the file or directory is readable. */ OUT UpnpFileInfo *info) { int code; struct stat s; FILE *fp; int rc = 0; time_t aux_LastModified; struct tm date; char buffer[ASCTIME_R_BUFFER_SIZE]; UpnpFileInfo_set_ContentType(info, NULL); code = stat(filename, &s); if (code == -1) return -1; if (S_ISDIR(s.st_mode)) UpnpFileInfo_set_IsDirectory(info, TRUE); else if (S_ISREG(s.st_mode)) UpnpFileInfo_set_IsDirectory(info, FALSE); else return -1; /* check readable */ fp = fopen(filename, "r"); UpnpFileInfo_set_IsReadable(info, fp != NULL); if (fp) fclose(fp); UpnpFileInfo_set_FileLength(info, s.st_size); UpnpFileInfo_set_LastModified(info, s.st_mtime); rc = get_content_type(filename, info); aux_LastModified = UpnpFileInfo_get_LastModified(info); UpnpPrintf(UPNP_INFO, HTTP, __FILE__, __LINE__, "file info: %s, length: %lld, last_mod=%s readable=%d\n", filename, (long long)UpnpFileInfo_get_FileLength(info), web_server_asctime_r(http_gmtime_r(&aux_LastModified, &date), buffer), UpnpFileInfo_get_IsReadable(info)); return rc; }
/*! * \brief Release memory allocated for the global web server root directory * and the global XML document. Resets the flag bWebServerState to * WEB_SERVER_DISABLED. * * \return Integer. */ static int get_file_info( /*! [in] Filename having the description document. */ const char *filename, /*! [out] File information object having file attributes such as filelength, * when was the file last modified, whether a file or a directory and * whether the file or directory is readable. */ struct File_Info *info) { int code; struct osal_stat_t s; OSAL_FILE *fp; int rc = 0; struct tm date; char buffer[ASCTIME_R_BUFFER_SIZE]; ixmlFreeDOMString(info->content_type); info->content_type = NULL; code = osal_stat(filename, &s); if (code == -1) return -1; if (S_ISDIR(s.st_mode)) info->is_directory = TRUE; else if (S_ISREG(s.st_mode)) info->is_directory = FALSE; else return -1; if (info->is_directory == FALSE) { fp = osal_fopen(filename, "r"); if (fp == NULL) return -1; osal_fclose(fp); /* check readable */ info->is_readable = 1; info->file_length = s.st_size; info->last_modified = s.st_mtime; rc = get_content_type(filename, &info->content_type); UpnpPrintf(UPNP_INFO, HTTP, __FILE__, __LINE__, "file info: %s, length: %lld, last_mod=%s readable=%d\n", filename, (long long)info->file_length, asctime_r(http_gmtime_r(&info->last_modified, &date), buffer), info->is_readable); } return rc; }
static void populate_mime_handlers (GMenu *menu, GbProjectFile *project_file) { g_autofree gchar *content_type = NULL; GList *list; GList *iter; GFile *file; g_assert (G_IS_MENU (menu)); g_assert (GB_IS_PROJECT_FILE (project_file)); g_menu_remove_all (menu); file = gb_project_file_get_file (project_file); if (file == NULL) return; content_type = get_content_type (file); if (content_type == NULL) return; list = g_app_info_get_all_for_type (content_type); for (iter = list; iter; iter = iter->next) { g_autoptr(GMenuItem) menu_item = NULL; g_autofree gchar *detailed_action = NULL; GAppInfo *app_info = iter->data; const gchar *display_name; const gchar *app_id; display_name = g_app_info_get_display_name (app_info); app_id = g_app_info_get_id (app_info); detailed_action = g_strdup_printf ("project-tree.open-with('%s')", app_id); menu_item = g_menu_item_new (display_name, detailed_action); g_menu_append_item (menu, menu_item); } g_list_free_full (list, g_object_unref); }
/** * maps a file to our address space * and returns it the calling function. */ void load_file(URL U, char *file) { FILE *fp; size_t len; char *buf; char *filename; char mode[8]; filename = trim(file); memset(mode, '\0', sizeof(mode)); snprintf(mode, sizeof(mode), "%s", (is_ascii(filename))?"r":"rb"); fp = fopen(filename, mode); if (! fp) { NOTIFY(ERROR, "unable to open file: %s", filename ); return; } fseek(fp, 0, SEEK_END); len = ftell(fp); fseek(fp, 0, SEEK_SET); buf = (char *)xmalloc(len+1); if ((fread(buf, 1, len, fp )) == len) { if (is_ascii(filename)) { buf[len] = '\0'; trim(buf); len = strlen(buf); } } else { NOTIFY(ERROR, "unable to read file: %s", filename ); } fclose(fp); if (len > 0) { url_set_conttype(U, get_content_type(filename)); url_set_postdata(U, buf, len); } xfree(buf); return; }
void set_content_type(const char* file_name, const char* key, aos_table_t *headers) { char *user_content_type = NULL; char *content_type = NULL; const char *mime_key = NULL; mime_key = file_name == NULL ? key : file_name; user_content_type = (char*)apr_table_get(headers, OSS_CONTENT_TYPE); if (NULL == user_content_type && mime_key != NULL) { content_type = get_content_type(mime_key); if (content_type) { apr_table_set(headers, OSS_CONTENT_TYPE, content_type); } else { apr_table_set(headers, OSS_CONTENT_TYPE, default_content_type); } } }
void load_file(URL U, char *file) { FILE *fp; size_t len = 0; struct stat st; char *filename; char postdata[POSTBUF]; size_t postlen = 0; filename = trim(file); memset(postdata, 0, POSTBUF); if ((lstat(filename, &st) == 0) || (errno != ENOENT)) { len = (st.st_size >= POSTBUF) ? POSTBUF : st.st_size; if (len < (unsigned)st.st_size) { NOTIFY(WARNING, "Truncated file: %s exceeds the post limit of %d bytes.\n", filename, POSTBUF); } if ((fp = fopen(filename, "r")) == NULL) { NOTIFY(ERROR, "could not open file: %s", filename); return; } if ((fread(postdata, 1, len, fp )) == len) { if (is_ascii(filename)) { trim(postdata); postlen = strlen(postdata); } else { postlen = len; } } else { NOTIFY(ERROR, "unable to read file: %s", filename ); } fclose(fp); } if (strlen(postdata) > 0) { url_set_conttype(U, get_content_type(filename)); url_set_postdata(U, postdata, postlen); } return; }
/** * maps a file to our address space * and returns it the calling function. */ void load_file(URL *U, char *file) { FILE *fp; size_t len = 0; struct stat st; char *filename; char postdata[POST_BUF]; unichar ubuf[POST_BUF+1]; size_t ulen; filename = trim(file); memset(postdata, 0, POST_BUF); if((lstat(filename, &st) == 0) || (errno != ENOENT)){ len = st.st_size; if((fp = fopen(filename, "r")) == NULL){ joe_error("could not open file: %s", filename); return; } if((fread(postdata, 1, len, fp )) == len){ if(looks_ascii((unsigned char*)postdata,len,ubuf,&ulen)) trim(postdata); else if(looks_utf8((unsigned char*)postdata,len,ubuf,&ulen)) trim(postdata); } else { joe_error( "unable to read file: %s", filename ); } fclose(fp); } if(strlen(postdata) > 0){ U->conttype = xstrdup(get_content_type(filename)); U->postlen = strlen(postdata); U->postdata = malloc(U->postlen); memcpy(U->postdata, postdata, U->postlen); U->postdata[U->postlen] = 0; } return; }
std::ostream& NodeXQType::serialize_ostream(std::ostream& os) const { store::StoreConsts::NodeKind node_kind = theNodeKind; xqtref_t content_type = get_content_type(); os << "[NodeXQType " << store::StoreConsts::toString(node_kind) << TypeOps::decode_quantifier(get_quantifier()); if (theNodeName != NULL) { os << " nametest=[uri: " << theNodeName->getNamespace() << ", local: " << theNodeName->getLocalName() << "]"; } if (content_type != NULL) { os << " content="; os << content_type->toString(); } return os << "]"; }
/* * This function encodes an arbitrary header into a chunk of bytes, * ready to be sent to the Application Server. * * The header codes start with this encoded-bytes: * 2: SIP-MSG-START based pointer to the header (including header name) * 2: length of the header * 1: length of the header name */ int encode_header(struct sip_msg *sipmsg,struct hdr_field *hdr,unsigned char *payload,int paylen) { int len=0; unsigned int integer,*methods=0; char *hdrstart,*tmp; unsigned short int ptr; struct to_body *tobody=0; struct via_body *viabody=0; struct cseq_body *cseqbody=0; char *msg,*myerror; int mlen; msg=sipmsg->buf; mlen=sipmsg->len; hdrstart = hdr->name.s; if(hdrstart-msg<0){ LM_ERR("header(%.*s) does not belong to sip_msg(hdrstart<msg)\n", hdr->name.len,hdr->name.s); return -1; } ptr=htons((short int)(hdrstart-msg)); if((hdrstart-msg)>mlen){ LM_ERR("out of the sip_msg bounds (%d>%d)\n",ntohs(ptr),mlen); return -1; } if(hdr->len>(1<<16)){ LM_ERR("length of header too long\n"); return -1; } memcpy(payload,&ptr,2); ptr=htons((short int)(hdr->len)); memcpy(payload+HEADER_LEN_IDX,&ptr,2); payload[HEADER_NAME_LEN_IDX]=(unsigned char)hdr->name.len; switch(hdr->type){ case HDR_FROM_T: case HDR_TO_T: case HDR_REFER_TO_T: case HDR_RPID_T: if(!hdr->parsed){ if((tobody=pkg_malloc(sizeof(struct to_body)))==0){ myerror="Out of memory !!\n"; goto error; } parse_to(hdr->body.s,hdr->body.s+hdr->body.len+1,tobody); if (tobody->error == PARSE_ERROR) { myerror="bad (REFER,TO,FROM,RPID) header\n"; pkg_free(tobody); return 5; goto error; } hdr->parsed=(struct to_body*)tobody; }else tobody=(struct to_body*)hdr->parsed; if((len=encode_to_body(hdr->name.s,hdr->len,tobody,payload+5))<0){ myerror="parsing from or to header\n"; goto error; }else{ return 5+len; } break; case HDR_CONTACT_T: if(!hdr->parsed) if(parse_contact(hdr)<0){ myerror="parsing contact\n"; goto error; } if((len=encode_contact_body(hdr->name.s,hdr->len,(contact_body_t*)hdr->parsed,payload+5))<0){ myerror="encoding contact header\n"; goto error; }else{ return 5+len; } break; case HDR_ROUTE_T: case HDR_RECORDROUTE_T: if(!hdr->parsed) if(parse_rr(hdr)<0){ myerror="encoding route or recordroute\n"; goto error; } if((len=encode_route_body(hdr->name.s,hdr->len,(rr_t*)hdr->parsed,payload+5))<0){ myerror="encoding route or recordroute header\n"; goto error; }else{ return 5+len; } break; case HDR_CONTENTLENGTH_T: if(!hdr->parsed){ tmp=parse_content_length(hdr->body.s,hdr->body.s+hdr->body.len+1,(int*)&integer); if (tmp==0){ myerror="bad content_length header\n"; goto error; } hdr->parsed=(void*)(long)integer; } if((len=encode_contentlength(hdr->name.s,hdr->len,(long int)hdr->parsed,(char*)(payload+5)))<0){ myerror="encoding content-length header\n"; goto error; }else{ return 5+len; } break; case HDR_VIA_T: if(!hdr->parsed){ if((viabody=pkg_malloc(sizeof(struct via_body)))==0){ myerror="out of memory\n"; goto error; } memset(viabody,0,sizeof(struct via_body)); if(parse_via(hdr->body.s,hdr->body.s+hdr->body.len+1,viabody)==0){ myerror="encoding via \n"; goto error; } hdr->parsed=viabody; } if((len=encode_via_body(hdr->name.s,hdr->len,(struct via_body*)hdr->parsed,payload+5))<0){ myerror="encoding via header\n"; goto error; }else{ return 5+len; } break; case HDR_ACCEPT_T: if(!hdr->parsed){ if(parse_accept_hdr(sipmsg)<0){ return 5; } } if((len=encode_accept(hdr->name.s,hdr->len,(unsigned int*)hdr->parsed,(char*)(payload+5)))<0){ myerror="encoding via header\n"; goto error; }else{ return 5+len; } break; case HDR_CONTENTTYPE_T: if(!hdr->parsed){ if(parse_content_type_hdr(sipmsg)<0){ myerror="encoding content-type header\n"; goto error; } } if((len=encode_content_type(hdr->name.s,hdr->len,(unsigned int)get_content_type(sipmsg),(char*)(payload+5)))<0){ myerror="encoding via header\n"; goto error; }else{ return 5+len; } break; case HDR_CSEQ_T: if(!hdr->parsed){ if((cseqbody=pkg_malloc(sizeof(struct cseq_body)))==0){ myerror="out of memory\n"; goto error; } memset(cseqbody,0,sizeof(struct cseq_body)); if(parse_cseq(hdr->name.s,hdr->body.s+hdr->body.len+1,cseqbody)==0){ myerror="encoding cseq header\n"; goto error; } hdr->parsed=cseqbody; } if((len=encode_cseq(hdr->name.s,hdr->len,(struct cseq_body*)hdr->parsed,payload+5))<0){ myerror="encoding via header\n"; goto error; }else{ return 5+len; } break; case HDR_EXPIRES_T: if(!hdr->parsed){ if(parse_expires(hdr)<0){ myerror="encoding expires header\n"; goto error; } } if((len=encode_expires(hdr->name.s,hdr->len,(exp_body_t *)hdr->parsed,payload+5))<0){ myerror="encoding expires header\n"; goto error; }else{ return 5+len; } break; case HDR_ALLOW_T: if(!hdr->parsed){ if((methods=pkg_malloc(sizeof(unsigned int)))==0){ myerror="out of memory\n"; goto error; } *methods=0; if(parse_methods(&hdr->body,methods)!=0){ myerror="encoding allow header\n"; pkg_free(methods); return 5; /*goto error;*/ } hdr->parsed=methods; } if((len=encode_allow(hdr->name.s,hdr->len,(unsigned int*)hdr->parsed,(char*)(payload+5)))<0){ myerror="encoding allow header\n"; goto error; }else{ return 5+len; } break; case HDR_AUTHORIZATION_T: case HDR_PROXYAUTH_T: if(!hdr->parsed){ if(parse_credentials(hdr)<0){ myerror="encoding a digest header\n"; goto error; } } if((len=encode_digest(hdr->name.s,hdr->len,(dig_cred_t*)(&(((auth_body_t*)hdr->parsed)->digest)),payload+5))<0){ myerror="encoding allow header\n"; goto error; }else{ return 5+len; } break; default: return 5; } return 1; error: if(tobody) pkg_free(tobody); if(cseqbody) pkg_free(cseqbody); if(viabody) free_via_list(viabody); if(methods) pkg_free(methods); LM_ERR("%s",myerror); return -1; }
/* * Processes the incoming request */ void process_request(gnutls_session_t session) { char buffer[MAX_BUF + 1]; char header[MAX_BUF]; /* * Reset mem, read the client header into * the buffer. */ memset (buffer, 0, MAX_BUF + 1); char *buf = read_line(session, buffer, MAX_BUF); printf("\t%s\n", buf); /* * Sepearate our first line request header * into separate parts, specifically we need * the file path its requesting */ char *token; char *tokenizer; token = strtok_r(buf, " ", &tokenizer); token = strtok_r(NULL, " ", &tokenizer); char *file_name = strdup(token); /* * If no file is listed, we default to * index.html */ if (strcmp(file_name, "/") == 0) strcpy(file_name, "/index.html"); /* * Setting where to serve content form */ char path[MAX_BUF]; snprintf(path, MAX_BUF, "content%s", file_name); /* * Opening the file, if it doesn't exist we stop here * and send a 404 Not found header to the client */ FILE *file = fopen(path, "r"); if (file == NULL) { fprintf(stderr, "\tFile not found.\n"); snprintf(header, MAX_BUF, "HTTP/1.1 404 Not Found\r\n\r\n"); if (gnutls_record_send(session, header, strlen(header)) < 0) err_exit(); } else { /* * File found, get the mime type */ char content_buffer[MAX_BUF]; char *mime = get_content_type(path, content_buffer, MAX_BUF); printf("\tContent type detected: %s\n", mime); /* * If it is PHP, we will close the currentl file descriptor * and set it to the execution of the script. The output * of the command is now our file descriptor */ if (strcmp(mime, "application/php") == 0) { printf("\tExecuting PHP file.\n"); fclose(file); snprintf(path, MAX_BUF, "php content%s", file_name); file = popen(path, "r"); if (file == NULL) err_exit(); // change the mime type. strcpy(mime, "text/html"); } /* * Check to see how big the file is */ fseek(file, 0, SEEK_END); int file_size = ftell(file); fseek(file, 0, SEEK_SET); /* * Read the content into here */ char *file_string = malloc(file_size+1); fread(file_string, 1, file_size, file); /* * Hotfix, PHP gives us a filesize of -1 apparently, * we need to use strlen to actually read the size of * the content from PHP */ if (file_size == -1) file_size = strlen(file_string); printf("\tFile size: %d\n", file_size); /* * Send the HTTP 200 OK header, the content type * and the content length to the browser. */ snprintf(header, MAX_BUF, "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: %s\r\nContent-Length: %d\r\n\r\n", mime, file_size); if (gnutls_record_send(session, header, strlen(header)) < 0) err_exit(); /* * Sending the content to the client * Needs to be chuncked because of the sending * limitations of gnutls. */ int bytes_read = 0; int bytes_sending; char *str_ptr = file_string; while (bytes_read < file_size) { if ((file_size - bytes_read) < MAX_BUF) bytes_sending = file_size - bytes_read; else bytes_sending = MAX_BUF; //printf("\tSent: %d bytes\n", bytes_sending); if (gnutls_record_send(session, str_ptr, bytes_sending) < 0) err_exit(); str_ptr += bytes_sending; bytes_read += bytes_sending; } } }
/** This is the main function from the HTTP server. Every new connection from a client is handled here. Here the request from the client is handled. */ void HTTPServer( GN::ClientList&, GN::CSocket & s ) { try { // buffer pentru cererea HTTP char buffer[ MAX_REQUEST_RECEIVE_BUFFER+2 ]; HttpRequest req; // Clasa pentru procesare cerere HTTP HttpUrl url; // Clasa pentru procesare URL HttpResponse r; // Clasa pentru raspunsul HTTP // receptioneaza cerere int ret = s.receive( buffer, MAX_REQUEST_RECEIVE_BUFFER ); buffer[ret]=0; // proceseaza cererea primita req.parse( buffer, ret ); check_for_errors( s, req ); // parse url url.parse( req.url(), WWW_FOLDER ); // Verifica daca exista fisierul FILE * file = fopen( url.file(), "r"); if( !file ) { r.redirect( STD_404_PAGE ); r.send(s); GSTD::CError error( 0, "File not found"); throw error; } fclose(file); // Trimite raspunsul ok - fisierul exista si continutul va fi trimis r.set_status( HTTP_OK ); r.set("Server", "GCWS 1.0"); r.set("Connection","close"); // incarca DLL // seteaza tipul continutului get_content_type( url.ext(), buffer ); r.set("Content-type", buffer ); // trimite raspunsul cu toate campurile setate corespunzator r.send(s); // send file - one buffer at time file = fopen( url.file(), "rb" ); int nb; do { nb = fread( buffer, 1, MAX_REQUEST_RECEIVE_BUFFER, file ); if( nb ) s.send( buffer, nb ); }while( nb ); fclose( file ); // if( url.is_set("debug") ) dump_request( req, url, s ); } catch( GSTD::CError error ) { throw error; } }
/*********************************************************************** * AtlAxCreateControlEx [atl100.@] * * REMARKS * See http://www.codeproject.com/com/cwebpage.asp for some background * */ HRESULT WINAPI AtlAxCreateControlEx(LPCOLESTR lpszName, HWND hWnd, IStream *pStream, IUnknown **ppUnkContainer, IUnknown **ppUnkControl, REFIID iidSink, IUnknown *punkSink) { CLSID controlId; HRESULT hRes; IOleObject *pControl; IUnknown *pUnkControl = NULL; IPersistStreamInit *pPSInit; IUnknown *pContainer = NULL; enum content content; TRACE("(%s %p %p %p %p %p %p)\n", debugstr_w(lpszName), hWnd, pStream, ppUnkContainer, ppUnkControl, iidSink, punkSink); if (ppUnkContainer) *ppUnkContainer = NULL; if (ppUnkControl) *ppUnkControl = NULL; content = get_content_type(lpszName, &controlId); if (content == IsEmpty) return S_OK; if (content == IsUnknown) return CO_E_CLASSSTRING; hRes = CoCreateInstance( &controlId, 0, CLSCTX_ALL, &IID_IOleObject, (void**) &pControl ); if ( FAILED( hRes ) ) { WARN( "cannot create ActiveX control %s instance - error 0x%08x\n", debugstr_guid( &controlId ), hRes ); return hRes; } hRes = IOleObject_QueryInterface( pControl, &IID_IPersistStreamInit, (void**) &pPSInit ); if ( SUCCEEDED( hRes ) ) { if (!pStream) IPersistStreamInit_InitNew( pPSInit ); else IPersistStreamInit_Load( pPSInit, pStream ); IPersistStreamInit_Release( pPSInit ); } else WARN("cannot get IID_IPersistStreamInit out of control\n"); IOleObject_QueryInterface( pControl, &IID_IUnknown, (void**) &pUnkControl ); IOleObject_Release( pControl ); hRes = AtlAxAttachControl( pUnkControl, hWnd, &pContainer ); if ( FAILED( hRes ) ) WARN("cannot attach control to window\n"); if ( content == IsURL ) { IWebBrowser2 *browser; hRes = IOleObject_QueryInterface( pControl, &IID_IWebBrowser2, (void**) &browser ); if ( !browser ) WARN( "Cannot query IWebBrowser2 interface: %08x\n", hRes ); else { VARIANT url; IWebBrowser2_put_Visible( browser, VARIANT_TRUE ); /* it seems that native does this on URL (but do not on MSHTML:! why? */ V_VT(&url) = VT_BSTR; V_BSTR(&url) = SysAllocString( lpszName ); hRes = IWebBrowser2_Navigate2( browser, &url, NULL, NULL, NULL, NULL ); if ( FAILED( hRes ) ) WARN( "IWebBrowser2::Navigate2 failed: %08x\n", hRes ); SysFreeString( V_BSTR(&url) ); IWebBrowser2_Release( browser ); } } if (ppUnkContainer) { *ppUnkContainer = pContainer; if ( pContainer ) IUnknown_AddRef( pContainer ); } if (ppUnkControl) { *ppUnkControl = pUnkControl; if ( pUnkControl ) IUnknown_AddRef( pUnkControl ); } if ( pUnkControl ) IUnknown_Release( pUnkControl ); if ( pContainer ) IUnknown_Release( pContainer ); return S_OK; }
/* Given the resource and socket, attempts * to locate the resource file and send it * down the connection, sends appropriate error * response if this fails. Returns OK if a response * is sent, CONNECTION_ERROR if unable to send * some sort of response back to the client */ status send_response(const char *file_name, int confd) { char http_200_ok[] = "HTTP/1.1 200 OK \r\n", content_length[] = "Content-Length: ", content_type[] = "Content-Type: ", length_buffer[11]; struct stat fs; int fd, file_size, result; const char *content_type_header; /* Simple check to avoid a directory traversal attack */ if (strstr(file_name, "..")) { printf("Unable to find/open file %s requested by client %d\n", file_name, confd); return send_not_found_response(confd); } /* Obtain file size to determine content length */ if ((fd = open(file_name, O_RDONLY)) == -1) { printf("Unable to find/open file %s requested by client %d\n", file_name, confd); return send_not_found_response(confd); } if (fstat(fd, &fs) == -1) { printf("Error obtaining file size of file:%sfor client %d\n. %s\n", file_name, confd, strerror(errno)); close(fd); return send_not_found_response(confd); } file_size = fs.st_size; content_type_header = get_content_type(file_name); /* Convert content-length to string */ snprintf(length_buffer, sizeof length_buffer, "%d", file_size); /* Send headers information */ write_all(confd, http_200_ok, strlen(http_200_ok)); write_all(confd, content_length, strlen(content_length)); write_all(confd, length_buffer, strlen(length_buffer)); write_all(confd, "\r\n", 2); write_all(confd, content_type, strlen(content_type)); write_all(confd, content_type_header, strlen(content_type_header)); write_all(confd, "\r\n\r\n", 4); /* Attempt to send the entire file over socket*/ result = send_file_socket(confd, fd, file_size); close(fd); if (result <= 0) { printf("Unable to write file:%s to client %d\n",file_name, confd); return CONNECTION_ERROR; } else if (result != file_size) { printf("Incorrect number of bytes written expected %d but was %d for" "file:%s requested by client %d\n",file_size, result, file_name, confd); return OK; } else { printf("File:%s successfully sent to client %d\n",file_name, confd); return OK; } return result; }
static int publish_presence(struct sip_msg* _m, struct presentity* presentity) { char *body = get_body(_m); int body_len = 0; int msg_expires = default_expires; time_t expires = 0; str etag; dbid_t generated_etag; int has_etag; presentity_info_t *p = NULL; int content_type = -1; if (_m->content_type) content_type = get_content_type(_m); if (_m->content_length) body_len = get_content_length(_m); if (_m->expires) { if (_m->expires->parsed) { msg_expires = ((exp_body_t*)_m->expires->parsed)->val; } } if (msg_expires > max_publish_expiration) msg_expires = max_publish_expiration; if (msg_expires != 0) expires = msg_expires + act_time; if (_m->sipifmatch) { if (_m->sipifmatch->parsed) etag = *(str*)_m->sipifmatch->parsed; else str_clear(&etag); has_etag = 1; } else { /* ETag was not set, generate a new one */ generate_dbid(generated_etag); etag.len = dbid_strlen(generated_etag); etag.s = dbid_strptr(generated_etag); has_etag = 0; } if (body_len > 0) { switch (content_type) { case MIMETYPE(APPLICATION,PIDFXML): if (parse_pidf_document(&p, body, body_len) != 0) { LOG(L_ERR, "can't parse PIDF document\n"); paerrno = PA_UNSUPP_DOC; /* ? PA_PARSE_ERR */ } break; case MIMETYPE(APPLICATION,CPIM_PIDFXML): if (parse_cpim_pidf_document(&p, body, body_len) != 0) { LOG(L_ERR, "can't parse CPIM-PIDF document\n"); paerrno = PA_UNSUPP_DOC; } break; default: LOG(L_ERR, "unsupported Content-Type 0x%x for PUBLISH handling\n", content_type); paerrno = PA_UNSUPP_DOC; } if (paerrno != PA_OK) return -1; } if (process_published_presentity_info(presentity, p, &etag, expires, has_etag) == 0) { /* add header fields into response */ add_expires_to_rpl(_m, msg_expires); add_etag_to_rpl(_m, &etag); } if (p) free_presentity_info(p); return 0; }
void* worker_main (void* arg) { int thread_id = *((int*)arg); free(arg); while (1) { void* item = queue_consume( &request_queue ); // this happens only if queue_consume_all() was called and no items remain. if( item == (void*)-1 ) { printf( "Worker exiting.\n" ); pthread_exit( (void*)NULL ); } request_t* req = (request_t*)item; char* buf = NULL; // if cache is enabled then try to get the cached entry. int size = config.cache_enabled ? get_cache( req->filename, &buf ) : -1; int cache_hit = (size != -1); // if it wasn't a hit, try to get the data from file. if( !cache_hit ) { size = get_file_data( req->filename, &buf ); if( size == -1 ) { // file does not exist. send result and move on. log_message( thread_id, req->req_num, req->fd, cache_hit, req->filename, 404); return_error( req->fd, "404 - File not found" ); continue; } // if cache is enabled then put the value into the cache. if( config.cache_enabled ) { if( put_cache( req->filename, buf, size ) == -1 ) { // error. I think we can keep going? fprintf( stderr, "error putting into cache %s\n", req->filename ); // do not escape. this is not a fatal error, so continue sending // request to client. } } } char* content_type = get_content_type( req->filename ); // send result and log. log_message(thread_id, req->req_num, req->fd, cache_hit, req->filename, size); if (return_result( req->fd, content_type, buf, size ) != 0) { fprintf(stderr, "Error returning request to client."); } // only put stuff into prefetch if cache is enabled. otherwise just free the request. if( config.cache_enabled ) { if( queue_produce( &prefetch_queue, req ) == -1 ) { fprintf( stderr, "Error putting request into prefetch queue '%s'\n", req->filename ); } } else { // free request created by dispatch. free( req->filename ); free( req ); // free the buffer. free( buf ); } } }